3 * Copyright 1998-2000 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>
26 # include <openssl/des.h>
27 #endif /* SLAPD_LMHASH */
41 #include <ac/unistd.h>
51 #ifdef HAVE_AIX_SECURITY
59 #include "lutil_md5.h"
60 #include "lutil_sha1.h"
63 static const unsigned char crypt64[] =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
68 typedef int (*PASSWD_CHK_FUNC)(
69 const struct pw_scheme *scheme,
70 const struct berval *passwd,
71 const struct berval *cred );
73 typedef struct berval * (*PASSWD_HASH_FUNC) (
74 const struct pw_scheme *scheme,
75 const struct berval *passwd );
79 PASSWD_CHK_FUNC chk_fn;
80 PASSWD_HASH_FUNC hash_fn;
83 /* password check routines */
85 const struct pw_scheme *scheme,
86 const struct berval *passwd,
87 const struct berval *cred );
90 const struct pw_scheme *scheme,
91 const struct berval *passwd,
92 const struct berval *cred );
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 );
105 static int chk_lanman(
106 const struct pw_scheme *scheme,
107 const struct berval *passwd,
108 const struct berval *cred );
113 const struct pw_scheme *scheme,
114 const struct berval *passwd,
115 const struct berval *cred );
119 static int chk_kerberos(
120 const struct pw_scheme *scheme,
121 const struct berval *passwd,
122 const struct berval *cred );
126 static int chk_crypt(
127 const struct pw_scheme *scheme,
128 const struct berval *passwd,
129 const struct berval *cred );
131 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
133 const struct pw_scheme *scheme,
134 const struct berval *passwd,
135 const struct berval *cred );
140 /* password hash routines */
141 static struct berval *hash_sha1(
142 const struct pw_scheme *scheme,
143 const struct berval *passwd );
145 static struct berval *hash_ssha1(
146 const struct pw_scheme *scheme,
147 const struct berval *passwd );
149 static struct berval *hash_smd5(
150 const struct pw_scheme *scheme,
151 const struct berval *passwd );
153 static struct berval *hash_md5(
154 const struct pw_scheme *scheme,
155 const struct berval *passwd );
158 static struct berval *hash_lanman(
159 const struct pw_scheme *scheme,
160 const struct berval *passwd );
164 static struct berval *hash_crypt(
165 const struct pw_scheme *scheme,
166 const struct berval *passwd );
170 static const struct pw_scheme pw_schemes[] =
172 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
173 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
175 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
176 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
179 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
180 #endif /* SLAPD_LMHASH */
183 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
187 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
191 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
192 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
193 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
197 #ifdef SLAPD_CLEARTEXT
199 { {0, "{CLEARTEXT}"}, NULL, NULL },
202 { {0, NULL}, NULL, NULL }
205 static const struct pw_scheme *get_scheme(
210 for( i=0; pw_schemes[i].name.bv_val; i++) {
211 if( pw_schemes[i].name.bv_len == 0 ) continue;
213 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
214 pw_schemes[i].name.bv_len) == 0 )
216 return &pw_schemes[i];
223 int lutil_passwd_scheme(
226 if( scheme == NULL ) {
230 return get_scheme(scheme) != NULL;
234 static int is_allowed_scheme(
236 const char** schemes )
240 if( schemes == NULL ) return 1;
242 for( i=0; schemes[i] != NULL; i++ ) {
243 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
250 static struct berval *passwd_scheme(
251 const struct pw_scheme *scheme,
252 const struct berval * passwd,
253 const char** allowed )
255 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
259 if( passwd->bv_len >= scheme->name.bv_len ) {
260 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
261 struct berval *bv = ber_memalloc( sizeof(struct berval) );
263 if( bv == NULL ) return NULL;
265 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
266 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
276 * Return 0 if creds are good.
280 const struct berval *passwd, /* stored passwd */
281 const struct berval *cred, /* user cred */
282 const char **schemes )
286 if (cred == NULL || cred->bv_len == 0 ||
287 passwd == NULL || passwd->bv_len == 0 )
292 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
293 if( pw_schemes[i].chk_fn ) {
294 struct berval *p = passwd_scheme( &pw_schemes[i],
298 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
300 /* only free the berval structure as the bv_val points
301 * into passwd->bv_val
310 #ifdef SLAPD_CLEARTEXT
311 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
312 return passwd->bv_len == cred->bv_len
313 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
320 struct berval * lutil_passwd_generate( ber_len_t len )
324 if( len < 1 ) return NULL;
326 pw = ber_memalloc( sizeof( struct berval ) );
327 if( pw == NULL ) return NULL;
330 pw->bv_val = ber_memalloc( len + 1 );
332 if( pw->bv_val == NULL ) {
337 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
342 for( len = 0; len < pw->bv_len; len++ ) {
343 pw->bv_val[len] = crypt64[
344 pw->bv_val[len] % (sizeof(crypt64)-1) ];
347 pw->bv_val[len] = '\0';
352 struct berval * lutil_passwd_hash(
353 const struct berval * passwd,
354 const char * method )
356 const struct pw_scheme *sc = get_scheme( method );
358 if( sc == NULL ) return NULL;
359 if( ! sc->hash_fn ) return NULL;
361 return (sc->hash_fn)( sc, passwd );
364 static struct berval * pw_string(
365 const struct pw_scheme *sc,
366 const struct berval *passwd )
368 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
369 if( pw == NULL ) return NULL;
371 pw->bv_len = sc->name.bv_len + passwd->bv_len;
372 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
374 if( pw->bv_val == NULL ) {
379 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
380 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
382 pw->bv_val[pw->bv_len] = '\0';
386 static struct berval * pw_string64(
387 const struct pw_scheme *sc,
388 const struct berval *hash,
389 const struct berval *salt )
392 struct berval string;
393 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
396 if( b64 == NULL ) return NULL;
399 /* need to base64 combined string */
400 string.bv_len = hash->bv_len + salt->bv_len;
401 string.bv_val = ber_memalloc( string.bv_len + 1 );
403 if( string.bv_val == NULL ) {
408 AC_MEMCPY( string.bv_val, hash->bv_val,
410 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
412 string.bv_val[string.bv_len] = '\0';
418 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
419 b64->bv_len = b64len + sc->name.bv_len;
420 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
422 if( b64->bv_val == NULL ) {
423 if( salt ) ber_memfree( string.bv_val );
428 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
431 string.bv_val, string.bv_len,
432 &b64->bv_val[sc->name.bv_len], b64len );
434 if( salt ) ber_memfree( string.bv_val );
441 /* recompute length */
442 b64->bv_len = sc->name.bv_len + rc;
443 assert( strlen(b64->bv_val) == b64->bv_len );
447 /* PASSWORD CHECK ROUTINES */
449 static int chk_ssha1(
450 const struct pw_scheme *sc,
451 const struct berval * passwd,
452 const struct berval * cred )
454 lutil_SHA1_CTX SHA1context;
455 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
457 unsigned char *orig_pass = NULL;
459 /* decode base64 password */
460 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
461 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
463 if( orig_pass == NULL ) return -1;
465 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
468 ber_memfree(orig_pass);
472 /* hash credentials with salt */
473 lutil_SHA1Init(&SHA1context);
474 lutil_SHA1Update(&SHA1context,
475 (const unsigned char *) cred->bv_val, cred->bv_len);
476 lutil_SHA1Update(&SHA1context,
477 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
478 rc - sizeof(SHA1digest));
479 lutil_SHA1Final(SHA1digest, &SHA1context);
482 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
483 ber_memfree(orig_pass);
488 const struct pw_scheme *sc,
489 const struct berval * passwd,
490 const struct berval * cred )
492 lutil_SHA1_CTX SHA1context;
493 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
495 unsigned char *orig_pass = NULL;
497 /* base64 un-encode password */
498 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
499 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
501 if( orig_pass == NULL ) return -1;
503 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
505 if( rc != sizeof(SHA1digest) ) {
506 ber_memfree(orig_pass);
510 /* hash credentials with salt */
511 lutil_SHA1Init(&SHA1context);
512 lutil_SHA1Update(&SHA1context,
513 (const unsigned char *) cred->bv_val, cred->bv_len);
514 lutil_SHA1Final(SHA1digest, &SHA1context);
517 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
518 ber_memfree(orig_pass);
523 const struct pw_scheme *sc,
524 const struct berval * passwd,
525 const struct berval * cred )
527 lutil_MD5_CTX MD5context;
528 unsigned char MD5digest[LUTIL_MD5_BYTES];
530 unsigned char *orig_pass = NULL;
532 /* base64 un-encode password */
533 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
534 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
536 if( orig_pass == NULL ) return -1;
538 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
540 ber_memfree(orig_pass);
544 /* hash credentials with salt */
545 lutil_MD5Init(&MD5context);
546 lutil_MD5Update(&MD5context,
547 (const unsigned char *) cred->bv_val,
549 lutil_MD5Update(&MD5context,
550 &orig_pass[sizeof(MD5digest)],
551 rc - sizeof(MD5digest));
552 lutil_MD5Final(MD5digest, &MD5context);
555 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
556 ber_memfree(orig_pass);
561 const struct pw_scheme *sc,
562 const struct berval * passwd,
563 const struct berval * cred )
565 lutil_MD5_CTX MD5context;
566 unsigned char MD5digest[LUTIL_MD5_BYTES];
568 unsigned char *orig_pass = NULL;
570 /* base64 un-encode password */
571 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
572 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
574 if( orig_pass == NULL ) return -1;
576 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
577 if ( rc != sizeof(MD5digest) ) {
578 ber_memfree(orig_pass);
582 /* hash credentials with salt */
583 lutil_MD5Init(&MD5context);
584 lutil_MD5Update(&MD5context,
585 (const unsigned char *) cred->bv_val,
587 lutil_MD5Final(MD5digest, &MD5context);
590 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
591 ber_memfree(orig_pass);
596 static int chk_lanman(
597 const struct pw_scheme *scheme,
598 const struct berval *passwd,
599 const struct berval *cred )
603 hash = hash_lanman( scheme, cred );
604 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
606 #endif /* SLAPD_LMHASH */
609 #ifdef HAVE_CYRUS_SASL
610 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
614 const struct pw_scheme *sc,
615 const struct berval * passwd,
616 const struct berval * cred )
621 for( i=0; i<cred->bv_len; i++) {
622 if(cred->bv_val[i] == '\0') {
623 return 1; /* NUL character in password */
627 if( cred->bv_val[i] != '\0' ) {
628 return 1; /* cred must behave like a string */
631 for( i=0; i<passwd->bv_len; i++) {
632 if(passwd->bv_val[i] == '\0') {
633 return 1; /* NUL character in password */
637 if( passwd->bv_val[i] != '\0' ) {
638 return 1; /* passwd must behave like a string */
643 #ifdef HAVE_CYRUS_SASL
644 if( lutil_passwd_sasl_conn != NULL ) {
645 const char *errstr = NULL;
648 sc = sasl_checkpass( lutil_passwd_sasl_conn,
649 passwd->bv_val, passwd->bv_len,
650 cred->bv_val, cred->bv_len,
653 rtn = ( sc != SASL_OK );
662 static int chk_kerberos(
663 const struct pw_scheme *sc,
664 const struct berval * passwd,
665 const struct berval * cred )
670 for( i=0; i<cred->bv_len; i++) {
671 if(cred->bv_val[i] == '\0') {
672 return 1; /* NUL character in password */
676 if( cred->bv_val[i] != '\0' ) {
677 return 1; /* cred must behave like a string */
680 for( i=0; i<passwd->bv_len; i++) {
681 if(passwd->bv_val[i] == '\0') {
682 return 1; /* NUL character in password */
686 if( passwd->bv_val[i] != '\0' ) {
687 return 1; /* passwd must behave like a string */
692 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
695 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
696 * (Royal Institute of Technology, Stockholm, Sweden).
697 * All rights reserved.
699 * Redistribution and use in source and binary forms, with or without
700 * modification, are permitted provided that the following conditions
703 * 1. Redistributions of source code must retain the above copyright
704 * notice, this list of conditions and the following disclaimer.
706 * 2. Redistributions in binary form must reproduce the above copyright
707 * notice, this list of conditions and the following disclaimer in the
708 * documentation and/or other materials provided with the distribution.
710 * 3. Neither the name of the Institute nor the names of its contributors
711 * may be used to endorse or promote products derived from this software
712 * without specific prior written permission.
714 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
715 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
716 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
717 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
718 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
719 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
720 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
721 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
722 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
723 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
727 krb5_context context;
730 krb5_get_init_creds_opt get_options;
731 krb5_verify_init_creds_opt verify_options;
732 krb5_principal client, server;
734 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
737 ret = krb5_init_context( &context );
743 krb5_get_init_creds_opt_set_preauth_list(&get_options,
747 krb5_get_init_creds_opt_init( &get_options );
749 krb5_verify_init_creds_opt_init( &verify_options );
751 ret = krb5_parse_name( context, passwd->bv_val, &client );
754 krb5_free_context( context );
758 ret = krb5_get_init_creds_password( context,
759 &creds, client, cred->bv_val, NULL,
760 NULL, 0, NULL, &get_options );
763 krb5_free_principal( context, client );
764 krb5_free_context( context );
769 char *host = ldap_pvt_get_fqdn( NULL );
772 krb5_free_principal( context, client );
773 krb5_free_context( context );
777 ret = krb5_sname_to_principal( context,
778 host, "ldap", KRB5_NT_SRV_HST, &server );
784 krb5_free_principal( context, client );
785 krb5_free_context( context );
789 ret = krb5_verify_init_creds( context,
790 &creds, server, NULL, NULL, &verify_options );
792 krb5_free_principal( context, client );
793 krb5_free_principal( context, server );
794 krb5_free_cred_contents( context, &creds );
795 krb5_free_context( context );
799 #elif defined(HAVE_KRB4)
801 /* Borrowed from Heimdal kpopper */
803 * Copyright (c) 1989 Regents of the University of California.
804 * All rights reserved. The Berkeley software License Agreement
805 * specifies the terms and conditions for redistribution.
809 char lrealm[REALM_SZ];
810 char tkt[MAXHOSTNAMELEN];
812 status = krb_get_lrealm(lrealm,1);
813 if (status == KFAILURE) {
817 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
818 TKT_ROOT, (unsigned)getpid());
819 krb_set_tkt_string (tkt);
821 status = krb_verify_user( passwd->bv_val, "", lrealm,
822 cred->bv_val, 1, "ldap");
824 dest_tkt(); /* no point in keeping the tickets */
826 return status == KFAILURE;
832 #endif /* SLAPD_KPASSWD */
835 static int chk_crypt(
836 const struct pw_scheme *sc,
837 const struct berval * passwd,
838 const struct berval * cred )
843 for( i=0; i<cred->bv_len; i++) {
844 if(cred->bv_val[i] == '\0') {
845 return 1; /* NUL character in password */
849 if( cred->bv_val[i] != '\0' ) {
850 return -1; /* cred must behave like a string */
853 if( passwd->bv_len < 2 ) {
854 return -1; /* passwd must be at least two characters long */
857 for( i=0; i<passwd->bv_len; i++) {
858 if(passwd->bv_val[i] == '\0') {
859 return -1; /* NUL character in password */
863 if( passwd->bv_val[i] != '\0' ) {
864 return -1; /* passwd must behave like a string */
867 cr = crypt( cred->bv_val, passwd->bv_val );
869 if( cr == NULL || cr[0] == '\0' ) {
870 /* salt must have been invalid */
874 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
877 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
879 const struct pw_scheme *sc,
880 const struct berval * passwd,
881 const struct berval * cred )
886 for( i=0; i<cred->bv_len; i++) {
887 if(cred->bv_val[i] == '\0') {
888 return -1; /* NUL character in password */
891 if( cred->bv_val[i] != '\0' ) {
892 return -1; /* cred must behave like a string */
895 for( i=0; i<passwd->bv_len; i++) {
896 if(passwd->bv_val[i] == '\0') {
897 return -1; /* NUL character in password */
901 if( passwd->bv_val[i] != '\0' ) {
902 return -1; /* passwd must behave like a string */
906 struct passwd *pwd = getpwnam(passwd->bv_val);
909 return -1; /* not found */
914 # ifdef HAVE_GETSPNAM
916 struct spwd *spwd = getspnam(passwd->bv_val);
923 # ifdef HAVE_AIX_SECURITY
925 struct userpw *upw = getuserpw(passwd->bv_val);
928 pw = upw->upw_passwd;
933 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
934 /* password must must be at least two characters long */
938 cr = crypt(cred->bv_val, pw);
940 if( cr == NULL || cr[0] == '\0' ) {
941 /* salt must have been invalid */
945 return strcmp(pw, cr) ? 1 : 0;
951 /* PASSWORD GENERATION ROUTINES */
953 static struct berval *hash_ssha1(
954 const struct pw_scheme *scheme,
955 const struct berval *passwd )
957 lutil_SHA1_CTX SHA1context;
958 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
959 unsigned char saltdata[4];
960 struct berval digest;
963 digest.bv_val = SHA1digest;
964 digest.bv_len = sizeof(SHA1digest);
965 salt.bv_val = saltdata;
966 salt.bv_len = sizeof(saltdata);
968 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
972 lutil_SHA1Init( &SHA1context );
973 lutil_SHA1Update( &SHA1context,
974 (const unsigned char *)passwd->bv_val, passwd->bv_len );
975 lutil_SHA1Update( &SHA1context,
976 (const unsigned char *)salt.bv_val, salt.bv_len );
977 lutil_SHA1Final( SHA1digest, &SHA1context );
979 return pw_string64( scheme, &digest, &salt);
982 static struct berval *hash_sha1(
983 const struct pw_scheme *scheme,
984 const struct berval *passwd )
986 lutil_SHA1_CTX SHA1context;
987 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
988 struct berval digest;
989 digest.bv_val = SHA1digest;
990 digest.bv_len = sizeof(SHA1digest);
992 lutil_SHA1Init( &SHA1context );
993 lutil_SHA1Update( &SHA1context,
994 (const unsigned char *)passwd->bv_val, passwd->bv_len );
995 lutil_SHA1Final( SHA1digest, &SHA1context );
997 return pw_string64( scheme, &digest, NULL);
1000 static struct berval *hash_smd5(
1001 const struct pw_scheme *scheme,
1002 const struct berval *passwd )
1004 lutil_MD5_CTX MD5context;
1005 unsigned char MD5digest[LUTIL_MD5_BYTES];
1006 unsigned char saltdata[4];
1007 struct berval digest;
1010 digest.bv_val = MD5digest;
1011 digest.bv_len = sizeof(MD5digest);
1012 salt.bv_val = saltdata;
1013 salt.bv_len = sizeof(saltdata);
1015 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1019 lutil_MD5Init( &MD5context );
1020 lutil_MD5Update( &MD5context,
1021 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1022 lutil_MD5Update( &MD5context,
1023 (const unsigned char *) salt.bv_val, salt.bv_len );
1024 lutil_MD5Final( MD5digest, &MD5context );
1026 return pw_string64( scheme, &digest, &salt );
1029 static struct berval *hash_md5(
1030 const struct pw_scheme *scheme,
1031 const struct berval *passwd )
1033 lutil_MD5_CTX MD5context;
1034 unsigned char MD5digest[LUTIL_MD5_BYTES];
1036 struct berval digest;
1038 digest.bv_val = MD5digest;
1039 digest.bv_len = sizeof(MD5digest);
1041 lutil_MD5Init( &MD5context );
1042 lutil_MD5Update( &MD5context,
1043 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1044 lutil_MD5Final( MD5digest, &MD5context );
1046 return pw_string64( scheme, &digest, NULL );
1051 /* pseudocode from RFC2433
1052 * A.2 LmPasswordHash()
1055 * IN 0-to-14-oem-char Password,
1056 * OUT 16-octet PasswordHash )
1058 * Set UcasePassword to the uppercased Password
1059 * Zero pad UcasePassword to 14 characters
1061 * DesHash( 1st 7-octets of UcasePassword,
1062 * giving 1st 8-octets of PasswordHash )
1064 * DesHash( 2nd 7-octets of UcasePassword,
1065 * giving 2nd 8-octets of PasswordHash )
1073 * OUT 8-octet Cypher )
1076 * * Make Cypher an irreversibly encrypted form of Clear by
1077 * * encrypting known text using Clear as the secret key.
1078 * * The known text consists of the string
1083 * Set StdText to "KGS!@#$%"
1084 * DesEncrypt( StdText, Clear, giving Cypher )
1093 * OUT 8-octet Cypher )
1096 * * Use the DES encryption algorithm [4] in ECB mode [9]
1097 * * to encrypt Clear into Cypher such that Cypher can
1098 * * only be decrypted back to Clear by providing Key.
1099 * * Note that the DES algorithm takes as input a 64-bit
1100 * * stream where the 8th, 16th, 24th, etc. bits are
1101 * * parity bits ignored by the encrypting algorithm.
1102 * * Unless you write your own DES to accept 56-bit input
1103 * * without parity, you will need to insert the parity bits
1109 static void lmPasswd_to_key(
1110 const unsigned char *lmPasswd,
1113 /* make room for parity bits */
1114 ((char *)key)[0] = lmPasswd[0];
1115 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1116 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1117 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1118 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1119 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1120 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1121 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1123 des_set_odd_parity( key );
1126 static struct berval *hash_lanman(
1127 const struct pw_scheme *scheme,
1128 const struct berval *passwd )
1132 char UcasePassword[15];
1134 des_key_schedule schedule;
1135 des_cblock StdText = "KGS!@#$%";
1136 des_cblock hash1, hash2;
1140 for( i=0; i<passwd->bv_len; i++) {
1141 if(passwd->bv_val[i] == '\0') {
1142 return NULL; /* NUL character in password */
1146 if( passwd->bv_val[i] != '\0' ) {
1147 return NULL; /* passwd must behave like a string */
1150 strncpy( UcasePassword, passwd->bv_val, 14 );
1151 UcasePassword[14] = '\0';
1152 ldap_pvt_str2upper( UcasePassword );
1154 lmPasswd_to_key( UcasePassword, &key );
1155 des_set_key_unchecked( &key, schedule );
1156 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1158 lmPasswd_to_key( &UcasePassword[7], &key );
1159 des_set_key_unchecked( &key, schedule );
1160 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1162 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1163 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1164 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1166 hash.bv_val = lmhash;
1169 return pw_string( scheme, &hash );
1171 #endif /* SLAPD_LMHASH */
1174 static struct berval *hash_crypt(
1175 const struct pw_scheme *scheme,
1176 const struct berval *passwd )
1179 unsigned char salt[9]; /* salt suitable for anything */
1182 for( i=0; i<passwd->bv_len; i++) {
1183 if(passwd->bv_val[i] == '\0') {
1184 return NULL; /* NUL character in password */
1188 if( passwd->bv_val[i] != '\0' ) {
1189 return NULL; /* passwd must behave like a string */
1192 if( lutil_entropy( salt, 8) < 0 ) {
1196 for( i=0; i<8; i++ ) {
1197 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1201 hash.bv_val = crypt( passwd->bv_val, salt );
1203 if( hash.bv_val == NULL ) return NULL;
1205 hash.bv_len = strlen( hash.bv_val );
1207 if( hash.bv_len == 0 ) {
1211 return pw_string( scheme, &hash );