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>
26 /* KPASSWD/krb.h brings in a conflicting des.h so don't use both.
27 * configure currently requires OpenSSL to enable LMHASH. Obviously
28 * this requirement can be fulfilled by the KRB DES library as well.
30 #if defined(SLAPD_LMHASH) && !defined(SLAPD_KPASSWD)
31 # include <openssl/des.h>
32 #endif /* SLAPD_LMHASH */
35 # ifdef HAVE_SASL_SASL_H
36 # include <sasl/sasl.h>
50 # include <ac/crypt.h>
52 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
59 # ifdef HAVE_AIX_SECURITY
69 #include "lutil_md5.h"
70 #include "lutil_sha1.h"
73 static const unsigned char crypt64[] =
74 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
77 static char *salt_format = NULL;
82 typedef int (*PASSWD_CHK_FUNC)(
83 const struct pw_scheme *scheme,
84 const struct berval *passwd,
85 const struct berval *cred );
87 typedef struct berval * (*PASSWD_HASH_FUNC) (
88 const struct pw_scheme *scheme,
89 const struct berval *passwd );
93 PASSWD_CHK_FUNC chk_fn;
94 PASSWD_HASH_FUNC hash_fn;
97 /* password check routines */
99 const struct pw_scheme *scheme,
100 const struct berval *passwd,
101 const struct berval *cred );
104 const struct pw_scheme *scheme,
105 const struct berval *passwd,
106 const struct berval *cred );
108 #ifdef LUTIL_SHA1_BYTES
109 static int chk_ssha1(
110 const struct pw_scheme *scheme,
111 const struct berval *passwd,
112 const struct berval *cred );
115 const struct pw_scheme *scheme,
116 const struct berval *passwd,
117 const struct berval *cred );
121 static int chk_lanman(
122 const struct pw_scheme *scheme,
123 const struct berval *passwd,
124 const struct berval *cred );
129 const struct pw_scheme *scheme,
130 const struct berval *passwd,
131 const struct berval *cred );
135 static int chk_kerberos(
136 const struct pw_scheme *scheme,
137 const struct berval *passwd,
138 const struct berval *cred );
142 static int chk_crypt(
143 const struct pw_scheme *scheme,
144 const struct berval *passwd,
145 const struct berval *cred );
147 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
149 const struct pw_scheme *scheme,
150 const struct berval *passwd,
151 const struct berval *cred );
156 #ifdef LUTIL_SHA1_BYTES
157 /* password hash routines */
158 static struct berval *hash_sha1(
159 const struct pw_scheme *scheme,
160 const struct berval *passwd );
162 static struct berval *hash_ssha1(
163 const struct pw_scheme *scheme,
164 const struct berval *passwd );
167 static struct berval *hash_smd5(
168 const struct pw_scheme *scheme,
169 const struct berval *passwd );
171 static struct berval *hash_md5(
172 const struct pw_scheme *scheme,
173 const struct berval *passwd );
176 static struct berval *hash_lanman(
177 const struct pw_scheme *scheme,
178 const struct berval *passwd );
182 static struct berval *hash_crypt(
183 const struct pw_scheme *scheme,
184 const struct berval *passwd );
187 #ifdef SLAPD_CLEARTEXT
188 static struct berval *hash_clear(
189 const struct pw_scheme *scheme,
190 const struct berval *passwd );
193 static const struct pw_scheme pw_schemes[] =
195 #ifdef LUTIL_SHA1_BYTES
196 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
197 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
200 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
201 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
204 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
205 #endif /* SLAPD_LMHASH */
208 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
212 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
216 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
217 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
218 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
222 #ifdef SLAPD_CLEARTEXT
224 { {0, "{CLEARTEXT}"}, NULL, hash_clear },
227 { {0, NULL}, NULL, NULL }
230 static const struct pw_scheme *get_scheme(
235 for( i=0; pw_schemes[i].name.bv_val; i++) {
236 if( pw_schemes[i].name.bv_val == NULL ) continue;
238 if( strcasecmp(scheme, pw_schemes[i].name.bv_val ) == 0 ) {
239 return &pw_schemes[i];
246 int lutil_passwd_scheme(
249 if( scheme == NULL ) {
253 return get_scheme(scheme) != NULL;
257 static int is_allowed_scheme(
259 const char** schemes )
263 if( schemes == NULL ) return 1;
265 for( i=0; schemes[i] != NULL; i++ ) {
266 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
273 static struct berval *passwd_scheme(
274 const struct pw_scheme *scheme,
275 const struct berval * passwd,
277 const char** allowed )
279 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
283 if( passwd->bv_len >= scheme->name.bv_len ) {
284 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
285 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
286 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
296 * Return 0 if creds are good.
300 const struct berval *passwd, /* stored passwd */
301 const struct berval *cred, /* user cred */
302 const char **schemes )
306 if (cred == NULL || cred->bv_len == 0 ||
307 passwd == NULL || passwd->bv_len == 0 )
312 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
313 if( pw_schemes[i].chk_fn ) {
315 struct berval *p = passwd_scheme( &pw_schemes[i],
316 passwd, &x, schemes );
319 return (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
324 #ifdef SLAPD_CLEARTEXT
325 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
326 return (( passwd->bv_len == cred->bv_len ) &&
327 ( passwd->bv_val[0] != '{' /*'}'*/ ))
328 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
335 struct berval * lutil_passwd_generate( ber_len_t len )
339 if( len < 1 ) return NULL;
341 pw = ber_memalloc( sizeof( struct berval ) );
342 if( pw == NULL ) return NULL;
345 pw->bv_val = ber_memalloc( len + 1 );
347 if( pw->bv_val == NULL ) {
352 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
357 for( len = 0; len < pw->bv_len; len++ ) {
358 pw->bv_val[len] = crypt64[
359 pw->bv_val[len] % (sizeof(crypt64)-1) ];
362 pw->bv_val[len] = '\0';
367 struct berval * lutil_passwd_hash(
368 const struct berval * passwd,
369 const char * method )
371 const struct pw_scheme *sc = get_scheme( method );
373 if( sc == NULL ) return NULL;
374 if( ! sc->hash_fn ) return NULL;
376 return (sc->hash_fn)( sc, passwd );
379 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
380 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
381 static struct berval * pw_string(
382 const struct pw_scheme *sc,
383 const struct berval *passwd )
385 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
386 if( pw == NULL ) return NULL;
388 pw->bv_len = sc->name.bv_len + passwd->bv_len;
389 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
391 if( pw->bv_val == NULL ) {
396 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
397 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
399 pw->bv_val[pw->bv_len] = '\0';
402 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
404 static struct berval * pw_string64(
405 const struct pw_scheme *sc,
406 const struct berval *hash,
407 const struct berval *salt )
410 struct berval string;
411 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
414 if( b64 == NULL ) return NULL;
417 /* need to base64 combined string */
418 string.bv_len = hash->bv_len + salt->bv_len;
419 string.bv_val = ber_memalloc( string.bv_len + 1 );
421 if( string.bv_val == NULL ) {
426 AC_MEMCPY( string.bv_val, hash->bv_val,
428 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
430 string.bv_val[string.bv_len] = '\0';
436 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
437 b64->bv_len = b64len + sc->name.bv_len;
438 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
440 if( b64->bv_val == NULL ) {
441 if( salt ) ber_memfree( string.bv_val );
446 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
449 string.bv_val, string.bv_len,
450 &b64->bv_val[sc->name.bv_len], b64len );
452 if( salt ) ber_memfree( string.bv_val );
459 /* recompute length */
460 b64->bv_len = sc->name.bv_len + rc;
461 assert( strlen(b64->bv_val) == b64->bv_len );
465 /* PASSWORD CHECK ROUTINES */
467 #ifdef LUTIL_SHA1_BYTES
468 static int chk_ssha1(
469 const struct pw_scheme *sc,
470 const struct berval * passwd,
471 const struct berval * cred )
473 lutil_SHA1_CTX SHA1context;
474 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
476 unsigned char *orig_pass = NULL;
478 /* decode base64 password */
479 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
480 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
482 if( orig_pass == NULL ) return -1;
484 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
487 ber_memfree(orig_pass);
491 /* hash credentials with salt */
492 lutil_SHA1Init(&SHA1context);
493 lutil_SHA1Update(&SHA1context,
494 (const unsigned char *) cred->bv_val, cred->bv_len);
495 lutil_SHA1Update(&SHA1context,
496 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
497 rc - sizeof(SHA1digest));
498 lutil_SHA1Final(SHA1digest, &SHA1context);
501 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
502 ber_memfree(orig_pass);
507 const struct pw_scheme *sc,
508 const struct berval * passwd,
509 const struct berval * cred )
511 lutil_SHA1_CTX SHA1context;
512 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
514 unsigned char *orig_pass = NULL;
516 /* base64 un-encode password */
517 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
518 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
520 if( orig_pass == NULL ) return -1;
522 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
524 if( rc != sizeof(SHA1digest) ) {
525 ber_memfree(orig_pass);
529 /* hash credentials with salt */
530 lutil_SHA1Init(&SHA1context);
531 lutil_SHA1Update(&SHA1context,
532 (const unsigned char *) cred->bv_val, cred->bv_len);
533 lutil_SHA1Final(SHA1digest, &SHA1context);
536 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
537 ber_memfree(orig_pass);
543 const struct pw_scheme *sc,
544 const struct berval * passwd,
545 const struct berval * cred )
547 lutil_MD5_CTX MD5context;
548 unsigned char MD5digest[LUTIL_MD5_BYTES];
550 unsigned char *orig_pass = NULL;
552 /* base64 un-encode password */
553 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
554 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
556 if( orig_pass == NULL ) return -1;
558 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
560 ber_memfree(orig_pass);
564 /* hash credentials with salt */
565 lutil_MD5Init(&MD5context);
566 lutil_MD5Update(&MD5context,
567 (const unsigned char *) cred->bv_val,
569 lutil_MD5Update(&MD5context,
570 &orig_pass[sizeof(MD5digest)],
571 rc - sizeof(MD5digest));
572 lutil_MD5Final(MD5digest, &MD5context);
575 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
576 ber_memfree(orig_pass);
581 const struct pw_scheme *sc,
582 const struct berval * passwd,
583 const struct berval * cred )
585 lutil_MD5_CTX MD5context;
586 unsigned char MD5digest[LUTIL_MD5_BYTES];
588 unsigned char *orig_pass = NULL;
590 /* base64 un-encode password */
591 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
592 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
594 if( orig_pass == NULL ) return -1;
596 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
597 if ( rc != sizeof(MD5digest) ) {
598 ber_memfree(orig_pass);
602 /* hash credentials with salt */
603 lutil_MD5Init(&MD5context);
604 lutil_MD5Update(&MD5context,
605 (const unsigned char *) cred->bv_val,
607 lutil_MD5Final(MD5digest, &MD5context);
610 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
611 ber_memfree(orig_pass);
616 static int chk_lanman(
617 const struct pw_scheme *scheme,
618 const struct berval *passwd,
619 const struct berval *cred )
623 hash = hash_lanman( scheme, cred );
624 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
626 #endif /* SLAPD_LMHASH */
629 #ifdef HAVE_CYRUS_SASL
630 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
634 const struct pw_scheme *sc,
635 const struct berval * passwd,
636 const struct berval * cred )
641 for( i=0; i<cred->bv_len; i++) {
642 if(cred->bv_val[i] == '\0') {
643 return 1; /* NUL character in password */
647 if( cred->bv_val[i] != '\0' ) {
648 return 1; /* cred must behave like a string */
651 for( i=0; i<passwd->bv_len; i++) {
652 if(passwd->bv_val[i] == '\0') {
653 return 1; /* NUL character in password */
657 if( passwd->bv_val[i] != '\0' ) {
658 return 1; /* passwd must behave like a string */
663 #ifdef HAVE_CYRUS_SASL
664 if( lutil_passwd_sasl_conn != NULL ) {
666 # if SASL_VERSION_MAJOR < 2
667 const char *errstr = NULL;
668 sc = sasl_checkpass( lutil_passwd_sasl_conn,
669 passwd->bv_val, passwd->bv_len,
670 cred->bv_val, cred->bv_len,
673 sc = sasl_checkpass( lutil_passwd_sasl_conn,
674 passwd->bv_val, passwd->bv_len,
675 cred->bv_val, cred->bv_len );
677 rtn = ( sc != SASL_OK );
686 static int chk_kerberos(
687 const struct pw_scheme *sc,
688 const struct berval * passwd,
689 const struct berval * cred )
694 for( i=0; i<cred->bv_len; i++) {
695 if(cred->bv_val[i] == '\0') {
696 return 1; /* NUL character in password */
700 if( cred->bv_val[i] != '\0' ) {
701 return 1; /* cred must behave like a string */
704 for( i=0; i<passwd->bv_len; i++) {
705 if(passwd->bv_val[i] == '\0') {
706 return 1; /* NUL character in password */
710 if( passwd->bv_val[i] != '\0' ) {
711 return 1; /* passwd must behave like a string */
716 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
719 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
720 * (Royal Institute of Technology, Stockholm, Sweden).
721 * All rights reserved.
723 * Redistribution and use in source and binary forms, with or without
724 * modification, are permitted provided that the following conditions
727 * 1. Redistributions of source code must retain the above copyright
728 * notice, this list of conditions and the following disclaimer.
730 * 2. Redistributions in binary form must reproduce the above copyright
731 * notice, this list of conditions and the following disclaimer in the
732 * documentation and/or other materials provided with the distribution.
734 * 3. Neither the name of the Institute nor the names of its contributors
735 * may be used to endorse or promote products derived from this software
736 * without specific prior written permission.
738 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
739 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
740 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
741 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
742 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
743 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
744 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
745 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
746 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
747 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
751 krb5_context context;
754 krb5_get_init_creds_opt get_options;
755 krb5_verify_init_creds_opt verify_options;
756 krb5_principal client, server;
758 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
761 ret = krb5_init_context( &context );
767 krb5_get_init_creds_opt_set_preauth_list(&get_options,
771 krb5_get_init_creds_opt_init( &get_options );
773 krb5_verify_init_creds_opt_init( &verify_options );
775 ret = krb5_parse_name( context, passwd->bv_val, &client );
778 krb5_free_context( context );
782 ret = krb5_get_init_creds_password( context,
783 &creds, client, cred->bv_val, NULL,
784 NULL, 0, NULL, &get_options );
787 krb5_free_principal( context, client );
788 krb5_free_context( context );
793 char *host = ldap_pvt_get_fqdn( NULL );
796 krb5_free_principal( context, client );
797 krb5_free_context( context );
801 ret = krb5_sname_to_principal( context,
802 host, "ldap", KRB5_NT_SRV_HST, &server );
808 krb5_free_principal( context, client );
809 krb5_free_context( context );
813 ret = krb5_verify_init_creds( context,
814 &creds, server, NULL, NULL, &verify_options );
816 krb5_free_principal( context, client );
817 krb5_free_principal( context, server );
818 krb5_free_cred_contents( context, &creds );
819 krb5_free_context( context );
823 #elif defined(HAVE_KRB4)
825 /* Borrowed from Heimdal kpopper */
827 * Copyright (c) 1989 Regents of the University of California.
828 * All rights reserved. The Berkeley software License Agreement
829 * specifies the terms and conditions for redistribution.
833 char lrealm[REALM_SZ];
834 char tkt[MAXHOSTNAMELEN];
836 status = krb_get_lrealm(lrealm,1);
837 if (status == KFAILURE) {
841 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
842 TKT_ROOT, (unsigned)getpid());
843 krb_set_tkt_string (tkt);
845 status = krb_verify_user( passwd->bv_val, "", lrealm,
846 cred->bv_val, 1, "ldap");
848 dest_tkt(); /* no point in keeping the tickets */
850 return status == KFAILURE;
856 #endif /* SLAPD_KPASSWD */
859 static int chk_crypt(
860 const struct pw_scheme *sc,
861 const struct berval * passwd,
862 const struct berval * cred )
867 for( i=0; i<cred->bv_len; i++) {
868 if(cred->bv_val[i] == '\0') {
869 return 1; /* NUL character in password */
873 if( cred->bv_val[i] != '\0' ) {
874 return -1; /* cred must behave like a string */
877 if( passwd->bv_len < 2 ) {
878 return -1; /* passwd must be at least two characters long */
881 for( i=0; i<passwd->bv_len; i++) {
882 if(passwd->bv_val[i] == '\0') {
883 return -1; /* NUL character in password */
887 if( passwd->bv_val[i] != '\0' ) {
888 return -1; /* passwd must behave like a string */
891 cr = crypt( cred->bv_val, passwd->bv_val );
893 if( cr == NULL || cr[0] == '\0' ) {
894 /* salt must have been invalid */
898 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
901 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
903 const struct pw_scheme *sc,
904 const struct berval * passwd,
905 const struct berval * cred )
910 for( i=0; i<cred->bv_len; i++) {
911 if(cred->bv_val[i] == '\0') {
912 return -1; /* NUL character in password */
915 if( cred->bv_val[i] != '\0' ) {
916 return -1; /* cred must behave like a string */
919 for( i=0; i<passwd->bv_len; i++) {
920 if(passwd->bv_val[i] == '\0') {
921 return -1; /* NUL character in password */
925 if( passwd->bv_val[i] != '\0' ) {
926 return -1; /* passwd must behave like a string */
930 struct passwd *pwd = getpwnam(passwd->bv_val);
933 return -1; /* not found */
938 # ifdef HAVE_GETSPNAM
940 struct spwd *spwd = getspnam(passwd->bv_val);
947 # ifdef HAVE_AIX_SECURITY
949 struct userpw *upw = getuserpw(passwd->bv_val);
952 pw = upw->upw_passwd;
957 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
958 /* password must must be at least two characters long */
962 cr = crypt(cred->bv_val, pw);
964 if( cr == NULL || cr[0] == '\0' ) {
965 /* salt must have been invalid */
969 return strcmp(pw, cr) ? 1 : 0;
975 /* PASSWORD GENERATION ROUTINES */
977 #ifdef LUTIL_SHA1_BYTES
978 static struct berval *hash_ssha1(
979 const struct pw_scheme *scheme,
980 const struct berval *passwd )
982 lutil_SHA1_CTX SHA1context;
983 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
984 unsigned char saltdata[4];
985 struct berval digest;
988 digest.bv_val = SHA1digest;
989 digest.bv_len = sizeof(SHA1digest);
990 salt.bv_val = saltdata;
991 salt.bv_len = sizeof(saltdata);
993 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
997 lutil_SHA1Init( &SHA1context );
998 lutil_SHA1Update( &SHA1context,
999 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1000 lutil_SHA1Update( &SHA1context,
1001 (const unsigned char *)salt.bv_val, salt.bv_len );
1002 lutil_SHA1Final( SHA1digest, &SHA1context );
1004 return pw_string64( scheme, &digest, &salt);
1007 static struct berval *hash_sha1(
1008 const struct pw_scheme *scheme,
1009 const struct berval *passwd )
1011 lutil_SHA1_CTX SHA1context;
1012 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1013 struct berval digest;
1014 digest.bv_val = SHA1digest;
1015 digest.bv_len = sizeof(SHA1digest);
1017 lutil_SHA1Init( &SHA1context );
1018 lutil_SHA1Update( &SHA1context,
1019 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1020 lutil_SHA1Final( SHA1digest, &SHA1context );
1022 return pw_string64( scheme, &digest, NULL);
1026 static struct berval *hash_smd5(
1027 const struct pw_scheme *scheme,
1028 const struct berval *passwd )
1030 lutil_MD5_CTX MD5context;
1031 unsigned char MD5digest[LUTIL_MD5_BYTES];
1032 unsigned char saltdata[4];
1033 struct berval digest;
1036 digest.bv_val = MD5digest;
1037 digest.bv_len = sizeof(MD5digest);
1038 salt.bv_val = saltdata;
1039 salt.bv_len = sizeof(saltdata);
1041 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1045 lutil_MD5Init( &MD5context );
1046 lutil_MD5Update( &MD5context,
1047 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1048 lutil_MD5Update( &MD5context,
1049 (const unsigned char *) salt.bv_val, salt.bv_len );
1050 lutil_MD5Final( MD5digest, &MD5context );
1052 return pw_string64( scheme, &digest, &salt );
1055 static struct berval *hash_md5(
1056 const struct pw_scheme *scheme,
1057 const struct berval *passwd )
1059 lutil_MD5_CTX MD5context;
1060 unsigned char MD5digest[LUTIL_MD5_BYTES];
1062 struct berval digest;
1064 digest.bv_val = MD5digest;
1065 digest.bv_len = sizeof(MD5digest);
1067 lutil_MD5Init( &MD5context );
1068 lutil_MD5Update( &MD5context,
1069 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1070 lutil_MD5Final( MD5digest, &MD5context );
1072 return pw_string64( scheme, &digest, NULL );
1077 /* pseudocode from RFC2433
1078 * A.2 LmPasswordHash()
1081 * IN 0-to-14-oem-char Password,
1082 * OUT 16-octet PasswordHash )
1084 * Set UcasePassword to the uppercased Password
1085 * Zero pad UcasePassword to 14 characters
1087 * DesHash( 1st 7-octets of UcasePassword,
1088 * giving 1st 8-octets of PasswordHash )
1090 * DesHash( 2nd 7-octets of UcasePassword,
1091 * giving 2nd 8-octets of PasswordHash )
1099 * OUT 8-octet Cypher )
1102 * * Make Cypher an irreversibly encrypted form of Clear by
1103 * * encrypting known text using Clear as the secret key.
1104 * * The known text consists of the string
1109 * Set StdText to "KGS!@#$%"
1110 * DesEncrypt( StdText, Clear, giving Cypher )
1119 * OUT 8-octet Cypher )
1122 * * Use the DES encryption algorithm [4] in ECB mode [9]
1123 * * to encrypt Clear into Cypher such that Cypher can
1124 * * only be decrypted back to Clear by providing Key.
1125 * * Note that the DES algorithm takes as input a 64-bit
1126 * * stream where the 8th, 16th, 24th, etc. bits are
1127 * * parity bits ignored by the encrypting algorithm.
1128 * * Unless you write your own DES to accept 56-bit input
1129 * * without parity, you will need to insert the parity bits
1135 static void lmPasswd_to_key(
1136 const unsigned char *lmPasswd,
1139 /* make room for parity bits */
1140 ((char *)key)[0] = lmPasswd[0];
1141 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1142 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1143 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1144 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1145 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1146 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1147 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1149 des_set_odd_parity( key );
1152 static struct berval *hash_lanman(
1153 const struct pw_scheme *scheme,
1154 const struct berval *passwd )
1158 char UcasePassword[15];
1160 des_key_schedule schedule;
1161 des_cblock StdText = "KGS!@#$%";
1162 des_cblock hash1, hash2;
1166 for( i=0; i<passwd->bv_len; i++) {
1167 if(passwd->bv_val[i] == '\0') {
1168 return NULL; /* NUL character in password */
1172 if( passwd->bv_val[i] != '\0' ) {
1173 return NULL; /* passwd must behave like a string */
1176 strncpy( UcasePassword, passwd->bv_val, 14 );
1177 UcasePassword[14] = '\0';
1178 ldap_pvt_str2upper( UcasePassword );
1180 lmPasswd_to_key( UcasePassword, &key );
1181 des_set_key_unchecked( &key, schedule );
1182 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1184 lmPasswd_to_key( &UcasePassword[7], &key );
1185 des_set_key_unchecked( &key, schedule );
1186 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1188 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1189 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1190 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1192 hash.bv_val = lmhash;
1195 return pw_string( scheme, &hash );
1197 #endif /* SLAPD_LMHASH */
1200 static struct berval *hash_crypt(
1201 const struct pw_scheme *scheme,
1202 const struct berval *passwd )
1205 unsigned char salt[32]; /* salt suitable for most anything */
1208 for( i=0; i<passwd->bv_len; i++) {
1209 if(passwd->bv_val[i] == '\0') {
1210 return NULL; /* NUL character in password */
1214 if( passwd->bv_val[i] != '\0' ) {
1215 return NULL; /* passwd must behave like a string */
1218 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1222 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1223 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1225 salt[sizeof( salt ) - 1 ] = '\0';
1227 if( salt_format != NULL ) {
1228 /* copy the salt we made into entropy before snprintfing
1229 it back into the salt */
1230 char entropy[sizeof(salt)];
1231 strcpy( entropy, salt );
1232 snprintf( salt, sizeof(entropy), salt_format, entropy );
1235 hash.bv_val = crypt( passwd->bv_val, salt );
1237 if( hash.bv_val == NULL ) return NULL;
1239 hash.bv_len = strlen( hash.bv_val );
1241 if( hash.bv_len == 0 ) {
1245 return pw_string( scheme, &hash );
1249 int lutil_salt_format(const char *format)
1252 free( salt_format );
1254 salt_format = format != NULL ? strdup( format ) : NULL;
1260 #ifdef SLAPD_CLEARTEXT
1261 static struct berval *hash_clear(
1262 const struct pw_scheme *scheme,
1263 const struct berval *passwd )
1265 return ber_bvdup( (struct berval *) passwd );