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 );
184 static const struct pw_scheme pw_schemes[] =
186 #ifdef LUTIL_SHA1_BYTES
187 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
188 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
191 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
192 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
195 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
196 #endif /* SLAPD_LMHASH */
199 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
203 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
207 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
208 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
209 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
213 #ifdef SLAPD_CLEARTEXT
215 { {0, "{CLEARTEXT}"}, NULL, NULL },
218 { {0, NULL}, NULL, NULL }
221 static const struct pw_scheme *get_scheme(
226 for( i=0; pw_schemes[i].name.bv_val; i++) {
227 if( pw_schemes[i].name.bv_len == 0 ) continue;
229 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
230 pw_schemes[i].name.bv_len) == 0 )
232 return &pw_schemes[i];
239 int lutil_passwd_scheme(
242 if( scheme == NULL ) {
246 return get_scheme(scheme) != NULL;
250 static int is_allowed_scheme(
252 const char** schemes )
256 if( schemes == NULL ) return 1;
258 for( i=0; schemes[i] != NULL; i++ ) {
259 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
266 static struct berval *passwd_scheme(
267 const struct pw_scheme *scheme,
268 const struct berval * passwd,
269 const char** allowed )
271 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
275 if( passwd->bv_len >= scheme->name.bv_len ) {
276 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
277 struct berval *bv = ber_memalloc( sizeof(struct berval) );
279 if( bv == NULL ) return NULL;
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 )
302 if (cred == NULL || cred->bv_len == 0 ||
303 passwd == NULL || passwd->bv_len == 0 )
308 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
309 if( pw_schemes[i].chk_fn ) {
310 struct berval *p = passwd_scheme( &pw_schemes[i],
314 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
316 /* only free the berval structure as the bv_val points
317 * into passwd->bv_val
326 #ifdef SLAPD_CLEARTEXT
327 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
328 return passwd->bv_len == cred->bv_len
329 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
336 struct berval * lutil_passwd_generate( ber_len_t len )
340 if( len < 1 ) return NULL;
342 pw = ber_memalloc( sizeof( struct berval ) );
343 if( pw == NULL ) return NULL;
346 pw->bv_val = ber_memalloc( len + 1 );
348 if( pw->bv_val == NULL ) {
353 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
358 for( len = 0; len < pw->bv_len; len++ ) {
359 pw->bv_val[len] = crypt64[
360 pw->bv_val[len] % (sizeof(crypt64)-1) ];
363 pw->bv_val[len] = '\0';
368 struct berval * lutil_passwd_hash(
369 const struct berval * passwd,
370 const char * method )
372 const struct pw_scheme *sc = get_scheme( method );
374 if( sc == NULL ) return NULL;
375 if( ! sc->hash_fn ) return NULL;
377 return (sc->hash_fn)( sc, passwd );
380 /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
381 #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
382 static struct berval * pw_string(
383 const struct pw_scheme *sc,
384 const struct berval *passwd )
386 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
387 if( pw == NULL ) return NULL;
389 pw->bv_len = sc->name.bv_len + passwd->bv_len;
390 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
392 if( pw->bv_val == NULL ) {
397 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
398 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
400 pw->bv_val[pw->bv_len] = '\0';
403 #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
405 static struct berval * pw_string64(
406 const struct pw_scheme *sc,
407 const struct berval *hash,
408 const struct berval *salt )
411 struct berval string;
412 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
415 if( b64 == NULL ) return NULL;
418 /* need to base64 combined string */
419 string.bv_len = hash->bv_len + salt->bv_len;
420 string.bv_val = ber_memalloc( string.bv_len + 1 );
422 if( string.bv_val == NULL ) {
427 AC_MEMCPY( string.bv_val, hash->bv_val,
429 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
431 string.bv_val[string.bv_len] = '\0';
437 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
438 b64->bv_len = b64len + sc->name.bv_len;
439 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
441 if( b64->bv_val == NULL ) {
442 if( salt ) ber_memfree( string.bv_val );
447 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
450 string.bv_val, string.bv_len,
451 &b64->bv_val[sc->name.bv_len], b64len );
453 if( salt ) ber_memfree( string.bv_val );
460 /* recompute length */
461 b64->bv_len = sc->name.bv_len + rc;
462 assert( strlen(b64->bv_val) == b64->bv_len );
466 /* PASSWORD CHECK ROUTINES */
468 #ifdef LUTIL_SHA1_BYTES
469 static int chk_ssha1(
470 const struct pw_scheme *sc,
471 const struct berval * passwd,
472 const struct berval * cred )
474 lutil_SHA1_CTX SHA1context;
475 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
477 unsigned char *orig_pass = NULL;
479 /* decode base64 password */
480 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
481 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
483 if( orig_pass == NULL ) return -1;
485 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
488 ber_memfree(orig_pass);
492 /* hash credentials with salt */
493 lutil_SHA1Init(&SHA1context);
494 lutil_SHA1Update(&SHA1context,
495 (const unsigned char *) cred->bv_val, cred->bv_len);
496 lutil_SHA1Update(&SHA1context,
497 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
498 rc - sizeof(SHA1digest));
499 lutil_SHA1Final(SHA1digest, &SHA1context);
502 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
503 ber_memfree(orig_pass);
508 const struct pw_scheme *sc,
509 const struct berval * passwd,
510 const struct berval * cred )
512 lutil_SHA1_CTX SHA1context;
513 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
515 unsigned char *orig_pass = NULL;
517 /* base64 un-encode password */
518 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
519 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
521 if( orig_pass == NULL ) return -1;
523 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
525 if( rc != sizeof(SHA1digest) ) {
526 ber_memfree(orig_pass);
530 /* hash credentials with salt */
531 lutil_SHA1Init(&SHA1context);
532 lutil_SHA1Update(&SHA1context,
533 (const unsigned char *) cred->bv_val, cred->bv_len);
534 lutil_SHA1Final(SHA1digest, &SHA1context);
537 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
538 ber_memfree(orig_pass);
544 const struct pw_scheme *sc,
545 const struct berval * passwd,
546 const struct berval * cred )
548 lutil_MD5_CTX MD5context;
549 unsigned char MD5digest[LUTIL_MD5_BYTES];
551 unsigned char *orig_pass = NULL;
553 /* base64 un-encode password */
554 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
555 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
557 if( orig_pass == NULL ) return -1;
559 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
561 ber_memfree(orig_pass);
565 /* hash credentials with salt */
566 lutil_MD5Init(&MD5context);
567 lutil_MD5Update(&MD5context,
568 (const unsigned char *) cred->bv_val,
570 lutil_MD5Update(&MD5context,
571 &orig_pass[sizeof(MD5digest)],
572 rc - sizeof(MD5digest));
573 lutil_MD5Final(MD5digest, &MD5context);
576 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
577 ber_memfree(orig_pass);
582 const struct pw_scheme *sc,
583 const struct berval * passwd,
584 const struct berval * cred )
586 lutil_MD5_CTX MD5context;
587 unsigned char MD5digest[LUTIL_MD5_BYTES];
589 unsigned char *orig_pass = NULL;
591 /* base64 un-encode password */
592 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
593 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
595 if( orig_pass == NULL ) return -1;
597 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
598 if ( rc != sizeof(MD5digest) ) {
599 ber_memfree(orig_pass);
603 /* hash credentials with salt */
604 lutil_MD5Init(&MD5context);
605 lutil_MD5Update(&MD5context,
606 (const unsigned char *) cred->bv_val,
608 lutil_MD5Final(MD5digest, &MD5context);
611 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
612 ber_memfree(orig_pass);
617 static int chk_lanman(
618 const struct pw_scheme *scheme,
619 const struct berval *passwd,
620 const struct berval *cred )
624 hash = hash_lanman( scheme, cred );
625 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
627 #endif /* SLAPD_LMHASH */
630 #ifdef HAVE_CYRUS_SASL
631 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
635 const struct pw_scheme *sc,
636 const struct berval * passwd,
637 const struct berval * cred )
642 for( i=0; i<cred->bv_len; i++) {
643 if(cred->bv_val[i] == '\0') {
644 return 1; /* NUL character in password */
648 if( cred->bv_val[i] != '\0' ) {
649 return 1; /* cred must behave like a string */
652 for( i=0; i<passwd->bv_len; i++) {
653 if(passwd->bv_val[i] == '\0') {
654 return 1; /* NUL character in password */
658 if( passwd->bv_val[i] != '\0' ) {
659 return 1; /* passwd must behave like a string */
664 #ifdef HAVE_CYRUS_SASL
665 if( lutil_passwd_sasl_conn != NULL ) {
667 # if SASL_VERSION_MAJOR < 2
668 const char *errstr = NULL;
669 sc = sasl_checkpass( lutil_passwd_sasl_conn,
670 passwd->bv_val, passwd->bv_len,
671 cred->bv_val, cred->bv_len,
674 sc = sasl_checkpass( lutil_passwd_sasl_conn,
675 passwd->bv_val, passwd->bv_len,
676 cred->bv_val, cred->bv_len );
678 rtn = ( sc != SASL_OK );
687 static int chk_kerberos(
688 const struct pw_scheme *sc,
689 const struct berval * passwd,
690 const struct berval * cred )
695 for( i=0; i<cred->bv_len; i++) {
696 if(cred->bv_val[i] == '\0') {
697 return 1; /* NUL character in password */
701 if( cred->bv_val[i] != '\0' ) {
702 return 1; /* cred must behave like a string */
705 for( i=0; i<passwd->bv_len; i++) {
706 if(passwd->bv_val[i] == '\0') {
707 return 1; /* NUL character in password */
711 if( passwd->bv_val[i] != '\0' ) {
712 return 1; /* passwd must behave like a string */
717 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
720 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
721 * (Royal Institute of Technology, Stockholm, Sweden).
722 * All rights reserved.
724 * Redistribution and use in source and binary forms, with or without
725 * modification, are permitted provided that the following conditions
728 * 1. Redistributions of source code must retain the above copyright
729 * notice, this list of conditions and the following disclaimer.
731 * 2. Redistributions in binary form must reproduce the above copyright
732 * notice, this list of conditions and the following disclaimer in the
733 * documentation and/or other materials provided with the distribution.
735 * 3. Neither the name of the Institute nor the names of its contributors
736 * may be used to endorse or promote products derived from this software
737 * without specific prior written permission.
739 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
740 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
741 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
742 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
743 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
744 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
745 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
746 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
747 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
748 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
752 krb5_context context;
755 krb5_get_init_creds_opt get_options;
756 krb5_verify_init_creds_opt verify_options;
757 krb5_principal client, server;
759 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
762 ret = krb5_init_context( &context );
768 krb5_get_init_creds_opt_set_preauth_list(&get_options,
772 krb5_get_init_creds_opt_init( &get_options );
774 krb5_verify_init_creds_opt_init( &verify_options );
776 ret = krb5_parse_name( context, passwd->bv_val, &client );
779 krb5_free_context( context );
783 ret = krb5_get_init_creds_password( context,
784 &creds, client, cred->bv_val, NULL,
785 NULL, 0, NULL, &get_options );
788 krb5_free_principal( context, client );
789 krb5_free_context( context );
794 char *host = ldap_pvt_get_fqdn( NULL );
797 krb5_free_principal( context, client );
798 krb5_free_context( context );
802 ret = krb5_sname_to_principal( context,
803 host, "ldap", KRB5_NT_SRV_HST, &server );
809 krb5_free_principal( context, client );
810 krb5_free_context( context );
814 ret = krb5_verify_init_creds( context,
815 &creds, server, NULL, NULL, &verify_options );
817 krb5_free_principal( context, client );
818 krb5_free_principal( context, server );
819 krb5_free_cred_contents( context, &creds );
820 krb5_free_context( context );
824 #elif defined(HAVE_KRB4)
826 /* Borrowed from Heimdal kpopper */
828 * Copyright (c) 1989 Regents of the University of California.
829 * All rights reserved. The Berkeley software License Agreement
830 * specifies the terms and conditions for redistribution.
834 char lrealm[REALM_SZ];
835 char tkt[MAXHOSTNAMELEN];
837 status = krb_get_lrealm(lrealm,1);
838 if (status == KFAILURE) {
842 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
843 TKT_ROOT, (unsigned)getpid());
844 krb_set_tkt_string (tkt);
846 status = krb_verify_user( passwd->bv_val, "", lrealm,
847 cred->bv_val, 1, "ldap");
849 dest_tkt(); /* no point in keeping the tickets */
851 return status == KFAILURE;
857 #endif /* SLAPD_KPASSWD */
860 static int chk_crypt(
861 const struct pw_scheme *sc,
862 const struct berval * passwd,
863 const struct berval * cred )
868 for( i=0; i<cred->bv_len; i++) {
869 if(cred->bv_val[i] == '\0') {
870 return 1; /* NUL character in password */
874 if( cred->bv_val[i] != '\0' ) {
875 return -1; /* cred must behave like a string */
878 if( passwd->bv_len < 2 ) {
879 return -1; /* passwd must be at least two characters long */
882 for( i=0; i<passwd->bv_len; i++) {
883 if(passwd->bv_val[i] == '\0') {
884 return -1; /* NUL character in password */
888 if( passwd->bv_val[i] != '\0' ) {
889 return -1; /* passwd must behave like a string */
892 cr = crypt( cred->bv_val, passwd->bv_val );
894 if( cr == NULL || cr[0] == '\0' ) {
895 /* salt must have been invalid */
899 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
902 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
904 const struct pw_scheme *sc,
905 const struct berval * passwd,
906 const struct berval * cred )
911 for( i=0; i<cred->bv_len; i++) {
912 if(cred->bv_val[i] == '\0') {
913 return -1; /* NUL character in password */
916 if( cred->bv_val[i] != '\0' ) {
917 return -1; /* cred must behave like a string */
920 for( i=0; i<passwd->bv_len; i++) {
921 if(passwd->bv_val[i] == '\0') {
922 return -1; /* NUL character in password */
926 if( passwd->bv_val[i] != '\0' ) {
927 return -1; /* passwd must behave like a string */
931 struct passwd *pwd = getpwnam(passwd->bv_val);
934 return -1; /* not found */
939 # ifdef HAVE_GETSPNAM
941 struct spwd *spwd = getspnam(passwd->bv_val);
948 # ifdef HAVE_AIX_SECURITY
950 struct userpw *upw = getuserpw(passwd->bv_val);
953 pw = upw->upw_passwd;
958 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
959 /* password must must be at least two characters long */
963 cr = crypt(cred->bv_val, pw);
965 if( cr == NULL || cr[0] == '\0' ) {
966 /* salt must have been invalid */
970 return strcmp(pw, cr) ? 1 : 0;
976 /* PASSWORD GENERATION ROUTINES */
978 #ifdef LUTIL_SHA1_BYTES
979 static struct berval *hash_ssha1(
980 const struct pw_scheme *scheme,
981 const struct berval *passwd )
983 lutil_SHA1_CTX SHA1context;
984 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
985 unsigned char saltdata[4];
986 struct berval digest;
989 digest.bv_val = SHA1digest;
990 digest.bv_len = sizeof(SHA1digest);
991 salt.bv_val = saltdata;
992 salt.bv_len = sizeof(saltdata);
994 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
998 lutil_SHA1Init( &SHA1context );
999 lutil_SHA1Update( &SHA1context,
1000 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1001 lutil_SHA1Update( &SHA1context,
1002 (const unsigned char *)salt.bv_val, salt.bv_len );
1003 lutil_SHA1Final( SHA1digest, &SHA1context );
1005 return pw_string64( scheme, &digest, &salt);
1008 static struct berval *hash_sha1(
1009 const struct pw_scheme *scheme,
1010 const struct berval *passwd )
1012 lutil_SHA1_CTX SHA1context;
1013 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1014 struct berval digest;
1015 digest.bv_val = SHA1digest;
1016 digest.bv_len = sizeof(SHA1digest);
1018 lutil_SHA1Init( &SHA1context );
1019 lutil_SHA1Update( &SHA1context,
1020 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1021 lutil_SHA1Final( SHA1digest, &SHA1context );
1023 return pw_string64( scheme, &digest, NULL);
1027 static struct berval *hash_smd5(
1028 const struct pw_scheme *scheme,
1029 const struct berval *passwd )
1031 lutil_MD5_CTX MD5context;
1032 unsigned char MD5digest[LUTIL_MD5_BYTES];
1033 unsigned char saltdata[4];
1034 struct berval digest;
1037 digest.bv_val = MD5digest;
1038 digest.bv_len = sizeof(MD5digest);
1039 salt.bv_val = saltdata;
1040 salt.bv_len = sizeof(saltdata);
1042 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1046 lutil_MD5Init( &MD5context );
1047 lutil_MD5Update( &MD5context,
1048 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1049 lutil_MD5Update( &MD5context,
1050 (const unsigned char *) salt.bv_val, salt.bv_len );
1051 lutil_MD5Final( MD5digest, &MD5context );
1053 return pw_string64( scheme, &digest, &salt );
1056 static struct berval *hash_md5(
1057 const struct pw_scheme *scheme,
1058 const struct berval *passwd )
1060 lutil_MD5_CTX MD5context;
1061 unsigned char MD5digest[LUTIL_MD5_BYTES];
1063 struct berval digest;
1065 digest.bv_val = MD5digest;
1066 digest.bv_len = sizeof(MD5digest);
1068 lutil_MD5Init( &MD5context );
1069 lutil_MD5Update( &MD5context,
1070 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1071 lutil_MD5Final( MD5digest, &MD5context );
1073 return pw_string64( scheme, &digest, NULL );
1078 /* pseudocode from RFC2433
1079 * A.2 LmPasswordHash()
1082 * IN 0-to-14-oem-char Password,
1083 * OUT 16-octet PasswordHash )
1085 * Set UcasePassword to the uppercased Password
1086 * Zero pad UcasePassword to 14 characters
1088 * DesHash( 1st 7-octets of UcasePassword,
1089 * giving 1st 8-octets of PasswordHash )
1091 * DesHash( 2nd 7-octets of UcasePassword,
1092 * giving 2nd 8-octets of PasswordHash )
1100 * OUT 8-octet Cypher )
1103 * * Make Cypher an irreversibly encrypted form of Clear by
1104 * * encrypting known text using Clear as the secret key.
1105 * * The known text consists of the string
1110 * Set StdText to "KGS!@#$%"
1111 * DesEncrypt( StdText, Clear, giving Cypher )
1120 * OUT 8-octet Cypher )
1123 * * Use the DES encryption algorithm [4] in ECB mode [9]
1124 * * to encrypt Clear into Cypher such that Cypher can
1125 * * only be decrypted back to Clear by providing Key.
1126 * * Note that the DES algorithm takes as input a 64-bit
1127 * * stream where the 8th, 16th, 24th, etc. bits are
1128 * * parity bits ignored by the encrypting algorithm.
1129 * * Unless you write your own DES to accept 56-bit input
1130 * * without parity, you will need to insert the parity bits
1136 static void lmPasswd_to_key(
1137 const unsigned char *lmPasswd,
1140 /* make room for parity bits */
1141 ((char *)key)[0] = lmPasswd[0];
1142 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1143 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1144 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1145 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1146 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1147 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1148 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1150 des_set_odd_parity( key );
1153 static struct berval *hash_lanman(
1154 const struct pw_scheme *scheme,
1155 const struct berval *passwd )
1159 char UcasePassword[15];
1161 des_key_schedule schedule;
1162 des_cblock StdText = "KGS!@#$%";
1163 des_cblock hash1, hash2;
1167 for( i=0; i<passwd->bv_len; i++) {
1168 if(passwd->bv_val[i] == '\0') {
1169 return NULL; /* NUL character in password */
1173 if( passwd->bv_val[i] != '\0' ) {
1174 return NULL; /* passwd must behave like a string */
1177 strncpy( UcasePassword, passwd->bv_val, 14 );
1178 UcasePassword[14] = '\0';
1179 ldap_pvt_str2upper( UcasePassword );
1181 lmPasswd_to_key( UcasePassword, &key );
1182 des_set_key_unchecked( &key, schedule );
1183 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1185 lmPasswd_to_key( &UcasePassword[7], &key );
1186 des_set_key_unchecked( &key, schedule );
1187 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1189 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1190 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1191 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1193 hash.bv_val = lmhash;
1196 return pw_string( scheme, &hash );
1198 #endif /* SLAPD_LMHASH */
1201 static struct berval *hash_crypt(
1202 const struct pw_scheme *scheme,
1203 const struct berval *passwd )
1206 unsigned char salt[32]; /* salt suitable for most anything */
1209 for( i=0; i<passwd->bv_len; i++) {
1210 if(passwd->bv_val[i] == '\0') {
1211 return NULL; /* NUL character in password */
1215 if( passwd->bv_val[i] != '\0' ) {
1216 return NULL; /* passwd must behave like a string */
1219 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1223 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1224 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1226 salt[sizeof( salt ) - 1 ] = '\0';
1228 if( salt_format != NULL ) {
1229 /* copy the salt we made into entropy before snprintfing
1230 it back into the salt */
1231 char entropy[sizeof(salt)];
1232 strcpy( entropy, salt );
1233 snprintf( salt, sizeof(entropy), salt_format, entropy );
1236 hash.bv_val = crypt( passwd->bv_val, salt );
1238 if( hash.bv_val == NULL ) return NULL;
1240 hash.bv_len = strlen( hash.bv_val );
1242 if( hash.bv_len == 0 ) {
1246 return pw_string( scheme, &hash );
1250 int lutil_salt_format(const char *format)
1253 free( salt_format );
1255 salt_format = format != NULL ? strdup( format ) : NULL;