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 */
40 #include <ac/unistd.h>
49 #ifdef HAVE_AIX_SECURITY
57 #include "lutil_md5.h"
58 #include "lutil_sha1.h"
61 static const unsigned char crypt64[] =
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
66 typedef int (*PASSWD_CHK_FUNC)(
67 const struct pw_scheme *scheme,
68 const struct berval *passwd,
69 const struct berval *cred );
71 typedef struct berval * (*PASSWD_HASH_FUNC) (
72 const struct pw_scheme *scheme,
73 const struct berval *passwd );
77 PASSWD_CHK_FUNC chk_fn;
78 PASSWD_HASH_FUNC hash_fn;
81 /* password check routines */
83 const struct pw_scheme *scheme,
84 const struct berval *passwd,
85 const struct berval *cred );
88 const struct pw_scheme *scheme,
89 const struct berval *passwd,
90 const struct berval *cred );
93 const struct pw_scheme *scheme,
94 const struct berval *passwd,
95 const struct berval *cred );
98 const struct pw_scheme *scheme,
99 const struct berval *passwd,
100 const struct berval *cred );
103 static int chk_lanman(
104 const struct pw_scheme *scheme,
105 const struct berval *passwd,
106 const struct berval *cred );
111 const struct pw_scheme *scheme,
112 const struct berval *passwd,
113 const struct berval *cred );
117 static int chk_kerberos(
118 const struct pw_scheme *scheme,
119 const struct berval *passwd,
120 const struct berval *cred );
124 static int chk_crypt(
125 const struct pw_scheme *scheme,
126 const struct berval *passwd,
127 const struct berval *cred );
129 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
131 const struct pw_scheme *scheme,
132 const struct berval *passwd,
133 const struct berval *cred );
138 /* password hash routines */
139 static struct berval *hash_sha1(
140 const struct pw_scheme *scheme,
141 const struct berval *passwd );
143 static struct berval *hash_ssha1(
144 const struct pw_scheme *scheme,
145 const struct berval *passwd );
147 static struct berval *hash_smd5(
148 const struct pw_scheme *scheme,
149 const struct berval *passwd );
151 static struct berval *hash_md5(
152 const struct pw_scheme *scheme,
153 const struct berval *passwd );
156 static struct berval *hash_lanman(
157 const struct pw_scheme *scheme,
158 const struct berval *passwd );
162 static struct berval *hash_crypt(
163 const struct pw_scheme *scheme,
164 const struct berval *passwd );
168 static const struct pw_scheme pw_schemes[] =
170 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
171 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
173 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
174 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
177 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
178 #endif /* SLAPD_LMHASH */
181 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
185 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
189 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
190 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
191 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
195 #ifdef SLAPD_CLEARTEXT
197 { {0, "{CLEARTEXT}"}, NULL, NULL },
200 { {0, NULL}, NULL, NULL }
203 static const struct pw_scheme *get_scheme(
208 for( i=0; pw_schemes[i].name.bv_val; i++) {
209 if( pw_schemes[i].name.bv_len == 0 ) continue;
211 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
212 pw_schemes[i].name.bv_len) == 0 )
214 return &pw_schemes[i];
221 int lutil_passwd_scheme(
224 if( scheme == NULL ) {
228 return get_scheme(scheme) != NULL;
232 static int is_allowed_scheme(
234 const char** schemes )
238 if( schemes == NULL ) return 1;
240 for( i=0; schemes[i] != NULL; i++ ) {
241 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
248 static struct berval *passwd_scheme(
249 const struct pw_scheme *scheme,
250 const struct berval * passwd,
251 const char** allowed )
253 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
257 if( passwd->bv_len >= scheme->name.bv_len ) {
258 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
259 struct berval *bv = ber_memalloc( sizeof(struct berval) );
261 if( bv == NULL ) return NULL;
263 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
264 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
274 * Return 0 if creds are good.
278 const struct berval *passwd, /* stored passwd */
279 const struct berval *cred, /* user cred */
280 const char **schemes )
284 if (cred == NULL || cred->bv_len == 0 ||
285 passwd == NULL || passwd->bv_len == 0 )
290 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
291 if( pw_schemes[i].chk_fn ) {
292 struct berval *p = passwd_scheme( &pw_schemes[i],
296 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
298 /* only free the berval structure as the bv_val points
299 * into passwd->bv_val
308 #ifdef SLAPD_CLEARTEXT
309 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
310 return passwd->bv_len == cred->bv_len
311 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
318 struct berval * lutil_passwd_generate( ber_len_t len )
322 if( len < 1 ) return NULL;
324 pw = ber_memalloc( sizeof( struct berval ) );
325 if( pw == NULL ) return NULL;
328 pw->bv_val = ber_memalloc( len + 1 );
330 if( pw->bv_val == NULL ) {
335 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
340 for( len = 0; len < pw->bv_len; len++ ) {
341 pw->bv_val[len] = crypt64[
342 pw->bv_val[len] % (sizeof(crypt64)-1) ];
345 pw->bv_val[len] = '\0';
350 struct berval * lutil_passwd_hash(
351 const struct berval * passwd,
352 const char * method )
354 const struct pw_scheme *sc = get_scheme( method );
356 if( sc == NULL ) return NULL;
357 if( ! sc->hash_fn ) return NULL;
359 return (sc->hash_fn)( sc, passwd );
362 static struct berval * pw_string(
363 const struct pw_scheme *sc,
364 const struct berval *passwd )
366 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
367 if( pw == NULL ) return NULL;
369 pw->bv_len = sc->name.bv_len + passwd->bv_len;
370 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
372 if( pw->bv_val == NULL ) {
377 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
378 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
380 pw->bv_val[pw->bv_len] = '\0';
384 static struct berval * pw_string64(
385 const struct pw_scheme *sc,
386 const struct berval *hash,
387 const struct berval *salt )
390 struct berval string;
391 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
394 if( b64 == NULL ) return NULL;
397 /* need to base64 combined string */
398 string.bv_len = hash->bv_len + salt->bv_len;
399 string.bv_val = ber_memalloc( string.bv_len + 1 );
401 if( string.bv_val == NULL ) {
406 AC_MEMCPY( string.bv_val, hash->bv_val,
408 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
410 string.bv_val[string.bv_len] = '\0';
416 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
417 b64->bv_len = b64len + sc->name.bv_len;
418 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
420 if( b64->bv_val == NULL ) {
421 if( salt ) ber_memfree( string.bv_val );
426 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
429 string.bv_val, string.bv_len,
430 &b64->bv_val[sc->name.bv_len], b64len );
432 if( salt ) ber_memfree( string.bv_val );
439 /* recompute length */
440 b64->bv_len = sc->name.bv_len + rc;
441 assert( strlen(b64->bv_val) == b64->bv_len );
445 /* PASSWORD CHECK ROUTINES */
447 static int chk_ssha1(
448 const struct pw_scheme *sc,
449 const struct berval * passwd,
450 const struct berval * cred )
452 lutil_SHA1_CTX SHA1context;
453 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
455 unsigned char *orig_pass = NULL;
457 /* decode base64 password */
458 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
459 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
461 if( orig_pass == NULL ) return -1;
463 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
466 ber_memfree(orig_pass);
470 /* hash credentials with salt */
471 lutil_SHA1Init(&SHA1context);
472 lutil_SHA1Update(&SHA1context,
473 (const unsigned char *) cred->bv_val, cred->bv_len);
474 lutil_SHA1Update(&SHA1context,
475 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
476 rc - sizeof(SHA1digest));
477 lutil_SHA1Final(SHA1digest, &SHA1context);
480 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
481 ber_memfree(orig_pass);
486 const struct pw_scheme *sc,
487 const struct berval * passwd,
488 const struct berval * cred )
490 lutil_SHA1_CTX SHA1context;
491 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
493 unsigned char *orig_pass = NULL;
495 /* base64 un-encode password */
496 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
497 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
499 if( orig_pass == NULL ) return -1;
501 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
503 if( rc != sizeof(SHA1digest) ) {
504 ber_memfree(orig_pass);
508 /* hash credentials with salt */
509 lutil_SHA1Init(&SHA1context);
510 lutil_SHA1Update(&SHA1context,
511 (const unsigned char *) cred->bv_val, cred->bv_len);
512 lutil_SHA1Final(SHA1digest, &SHA1context);
515 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
516 ber_memfree(orig_pass);
521 const struct pw_scheme *sc,
522 const struct berval * passwd,
523 const struct berval * cred )
525 lutil_MD5_CTX MD5context;
526 unsigned char MD5digest[LUTIL_MD5_BYTES];
528 unsigned char *orig_pass = NULL;
530 /* base64 un-encode password */
531 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
532 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
534 if( orig_pass == NULL ) return -1;
536 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
538 ber_memfree(orig_pass);
542 /* hash credentials with salt */
543 lutil_MD5Init(&MD5context);
544 lutil_MD5Update(&MD5context,
545 (const unsigned char *) cred->bv_val,
547 lutil_MD5Update(&MD5context,
548 &orig_pass[sizeof(MD5digest)],
549 rc - sizeof(MD5digest));
550 lutil_MD5Final(MD5digest, &MD5context);
553 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
554 ber_memfree(orig_pass);
559 const struct pw_scheme *sc,
560 const struct berval * passwd,
561 const struct berval * cred )
563 lutil_MD5_CTX MD5context;
564 unsigned char MD5digest[LUTIL_MD5_BYTES];
566 unsigned char *orig_pass = NULL;
568 /* base64 un-encode password */
569 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
570 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
572 if( orig_pass == NULL ) return -1;
574 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
575 if ( rc != sizeof(MD5digest) ) {
576 ber_memfree(orig_pass);
580 /* hash credentials with salt */
581 lutil_MD5Init(&MD5context);
582 lutil_MD5Update(&MD5context,
583 (const unsigned char *) cred->bv_val,
585 lutil_MD5Final(MD5digest, &MD5context);
588 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
589 ber_memfree(orig_pass);
594 static int chk_lanman(
595 const struct pw_scheme *scheme,
596 const struct berval *passwd,
597 const struct berval *cred )
601 hash = hash_lanman( scheme, cred );
602 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
604 #endif /* SLAPD_LMHASH */
607 #ifdef HAVE_CYRUS_SASL
608 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
612 const struct pw_scheme *sc,
613 const struct berval * passwd,
614 const struct berval * cred )
619 for( i=0; i<cred->bv_len; i++) {
620 if(cred->bv_val[i] == '\0') {
621 return 1; /* NUL character in password */
625 if( cred->bv_val[i] != '\0' ) {
626 return 1; /* cred must behave like a string */
629 for( i=0; i<passwd->bv_len; i++) {
630 if(passwd->bv_val[i] == '\0') {
631 return 1; /* NUL character in password */
635 if( passwd->bv_val[i] != '\0' ) {
636 return 1; /* passwd must behave like a string */
641 #ifdef HAVE_CYRUS_SASL
642 if( lutil_passwd_sasl_conn != NULL ) {
643 const char *errstr = NULL;
646 sc = sasl_checkpass( lutil_passwd_sasl_conn,
647 passwd->bv_val, passwd->bv_len,
648 cred->bv_val, cred->bv_len,
651 rtn = ( sc != SASL_OK );
660 static int chk_kerberos(
661 const struct pw_scheme *sc,
662 const struct berval * passwd,
663 const struct berval * cred )
668 for( i=0; i<cred->bv_len; i++) {
669 if(cred->bv_val[i] == '\0') {
670 return 1; /* NUL character in password */
674 if( cred->bv_val[i] != '\0' ) {
675 return 1; /* cred must behave like a string */
678 for( i=0; i<passwd->bv_len; i++) {
679 if(passwd->bv_val[i] == '\0') {
680 return 1; /* NUL character in password */
684 if( passwd->bv_val[i] != '\0' ) {
685 return 1; /* passwd must behave like a string */
690 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
693 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
694 * (Royal Institute of Technology, Stockholm, Sweden).
695 * All rights reserved.
697 * Redistribution and use in source and binary forms, with or without
698 * modification, are permitted provided that the following conditions
701 * 1. Redistributions of source code must retain the above copyright
702 * notice, this list of conditions and the following disclaimer.
704 * 2. Redistributions in binary form must reproduce the above copyright
705 * notice, this list of conditions and the following disclaimer in the
706 * documentation and/or other materials provided with the distribution.
708 * 3. Neither the name of the Institute nor the names of its contributors
709 * may be used to endorse or promote products derived from this software
710 * without specific prior written permission.
712 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
713 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
714 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
715 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
716 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
717 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
718 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
719 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
720 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
721 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
725 krb5_context context;
728 krb5_get_init_creds_opt get_options;
729 krb5_verify_init_creds_opt verify_options;
730 krb5_principal client, server;
732 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
735 ret = krb5_init_context( &context );
741 krb5_get_init_creds_opt_set_preauth_list(&get_options,
745 krb5_get_init_creds_opt_init( &get_options );
747 krb5_verify_init_creds_opt_init( &verify_options );
749 ret = krb5_parse_name( context, passwd->bv_val, &client );
752 krb5_free_context( context );
756 ret = krb5_get_init_creds_password( context,
757 &creds, client, cred->bv_val, NULL,
758 NULL, 0, NULL, &get_options );
761 krb5_free_principal( context, client );
762 krb5_free_context( context );
767 char *host = ldap_pvt_get_fqdn( NULL );
770 krb5_free_principal( context, client );
771 krb5_free_context( context );
775 ret = krb5_sname_to_principal( context,
776 host, "ldap", KRB5_NT_SRV_HST, &server );
782 krb5_free_principal( context, client );
783 krb5_free_context( context );
787 ret = krb5_verify_init_creds( context,
788 &creds, server, NULL, NULL, &verify_options );
790 krb5_free_principal( context, client );
791 krb5_free_principal( context, server );
792 krb5_free_cred_contents( context, &creds );
793 krb5_free_context( context );
797 #elif defined(HAVE_KRB4)
799 /* Borrowed from Heimdal kpopper */
801 * Copyright (c) 1989 Regents of the University of California.
802 * All rights reserved. The Berkeley software License Agreement
803 * specifies the terms and conditions for redistribution.
807 char lrealm[REALM_SZ];
808 char tkt[MAXHOSTNAMELEN];
810 status = krb_get_lrealm(lrealm,1);
811 if (status == KFAILURE) {
815 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
816 TKT_ROOT, (unsigned)getpid());
817 krb_set_tkt_string (tkt);
819 status = krb_verify_user( passwd->bv_val, "", lrealm,
820 cred->bv_val, 1, "ldap");
822 dest_tkt(); /* no point in keeping the tickets */
824 return status == KFAILURE;
830 #endif /* SLAPD_KPASSWD */
833 static int chk_crypt(
834 const struct pw_scheme *sc,
835 const struct berval * passwd,
836 const struct berval * cred )
841 for( i=0; i<cred->bv_len; i++) {
842 if(cred->bv_val[i] == '\0') {
843 return 1; /* NUL character in password */
847 if( cred->bv_val[i] != '\0' ) {
848 return -1; /* cred must behave like a string */
851 if( passwd->bv_len < 2 ) {
852 return -1; /* passwd must be at least two characters long */
855 for( i=0; i<passwd->bv_len; i++) {
856 if(passwd->bv_val[i] == '\0') {
857 return -1; /* NUL character in password */
861 if( passwd->bv_val[i] != '\0' ) {
862 return -1; /* passwd must behave like a string */
865 cr = crypt( cred->bv_val, passwd->bv_val );
867 if( cr == NULL || cr[0] == '\0' ) {
868 /* salt must have been invalid */
872 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
875 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
877 const struct pw_scheme *sc,
878 const struct berval * passwd,
879 const struct berval * cred )
884 for( i=0; i<cred->bv_len; i++) {
885 if(cred->bv_val[i] == '\0') {
886 return -1; /* NUL character in password */
889 if( cred->bv_val[i] != '\0' ) {
890 return -1; /* cred must behave like a string */
893 for( i=0; i<passwd->bv_len; i++) {
894 if(passwd->bv_val[i] == '\0') {
895 return -1; /* NUL character in password */
899 if( passwd->bv_val[i] != '\0' ) {
900 return -1; /* passwd must behave like a string */
904 struct passwd *pwd = getpwnam(passwd->bv_val);
907 return -1; /* not found */
912 # ifdef HAVE_GETSPNAM
914 struct spwd *spwd = getspnam(passwd->bv_val);
921 # ifdef HAVE_AIX_SECURITY
923 struct userpw *upw = getuserpw(passwd->bv_val);
926 pw = upw->upw_passwd;
931 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
932 /* password must must be at least two characters long */
936 cr = crypt(cred->bv_val, pw);
938 if( cr == NULL || cr[0] == '\0' ) {
939 /* salt must have been invalid */
943 return strcmp(pw, cr) ? 1 : 0;
949 /* PASSWORD GENERATION ROUTINES */
951 static struct berval *hash_ssha1(
952 const struct pw_scheme *scheme,
953 const struct berval *passwd )
955 lutil_SHA1_CTX SHA1context;
956 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
957 unsigned char saltdata[4];
958 struct berval digest;
961 digest.bv_val = SHA1digest;
962 digest.bv_len = sizeof(SHA1digest);
963 salt.bv_val = saltdata;
964 salt.bv_len = sizeof(saltdata);
966 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
970 lutil_SHA1Init( &SHA1context );
971 lutil_SHA1Update( &SHA1context,
972 (const unsigned char *)passwd->bv_val, passwd->bv_len );
973 lutil_SHA1Update( &SHA1context,
974 (const unsigned char *)salt.bv_val, salt.bv_len );
975 lutil_SHA1Final( SHA1digest, &SHA1context );
977 return pw_string64( scheme, &digest, &salt);
980 static struct berval *hash_sha1(
981 const struct pw_scheme *scheme,
982 const struct berval *passwd )
984 lutil_SHA1_CTX SHA1context;
985 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
986 struct berval digest;
987 digest.bv_val = SHA1digest;
988 digest.bv_len = sizeof(SHA1digest);
990 lutil_SHA1Init( &SHA1context );
991 lutil_SHA1Update( &SHA1context,
992 (const unsigned char *)passwd->bv_val, passwd->bv_len );
993 lutil_SHA1Final( SHA1digest, &SHA1context );
995 return pw_string64( scheme, &digest, NULL);
998 static struct berval *hash_smd5(
999 const struct pw_scheme *scheme,
1000 const struct berval *passwd )
1002 lutil_MD5_CTX MD5context;
1003 unsigned char MD5digest[LUTIL_MD5_BYTES];
1004 unsigned char saltdata[4];
1005 struct berval digest;
1008 digest.bv_val = MD5digest;
1009 digest.bv_len = sizeof(MD5digest);
1010 salt.bv_val = saltdata;
1011 salt.bv_len = sizeof(saltdata);
1013 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1017 lutil_MD5Init( &MD5context );
1018 lutil_MD5Update( &MD5context,
1019 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1020 lutil_MD5Update( &MD5context,
1021 (const unsigned char *) salt.bv_val, salt.bv_len );
1022 lutil_MD5Final( MD5digest, &MD5context );
1024 return pw_string64( scheme, &digest, &salt );
1027 static struct berval *hash_md5(
1028 const struct pw_scheme *scheme,
1029 const struct berval *passwd )
1031 lutil_MD5_CTX MD5context;
1032 unsigned char MD5digest[LUTIL_MD5_BYTES];
1034 struct berval digest;
1036 digest.bv_val = MD5digest;
1037 digest.bv_len = sizeof(MD5digest);
1039 lutil_MD5Init( &MD5context );
1040 lutil_MD5Update( &MD5context,
1041 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1042 lutil_MD5Final( MD5digest, &MD5context );
1044 return pw_string64( scheme, &digest, NULL );
1049 /* pseudocode from RFC2433
1050 * A.2 LmPasswordHash()
1053 * IN 0-to-14-oem-char Password,
1054 * OUT 16-octet PasswordHash )
1056 * Set UcasePassword to the uppercased Password
1057 * Zero pad UcasePassword to 14 characters
1059 * DesHash( 1st 7-octets of UcasePassword,
1060 * giving 1st 8-octets of PasswordHash )
1062 * DesHash( 2nd 7-octets of UcasePassword,
1063 * giving 2nd 8-octets of PasswordHash )
1071 * OUT 8-octet Cypher )
1074 * * Make Cypher an irreversibly encrypted form of Clear by
1075 * * encrypting known text using Clear as the secret key.
1076 * * The known text consists of the string
1081 * Set StdText to "KGS!@#$%"
1082 * DesEncrypt( StdText, Clear, giving Cypher )
1091 * OUT 8-octet Cypher )
1094 * * Use the DES encryption algorithm [4] in ECB mode [9]
1095 * * to encrypt Clear into Cypher such that Cypher can
1096 * * only be decrypted back to Clear by providing Key.
1097 * * Note that the DES algorithm takes as input a 64-bit
1098 * * stream where the 8th, 16th, 24th, etc. bits are
1099 * * parity bits ignored by the encrypting algorithm.
1100 * * Unless you write your own DES to accept 56-bit input
1101 * * without parity, you will need to insert the parity bits
1107 static struct berval *hash_lanman(
1108 const struct pw_scheme *scheme,
1109 const struct berval *passwd )
1111 static void lmPasswd_to_key(const unsigned char *lmPasswd, des_cblock *key)
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 );
1127 char UcasePassword[15];
1129 des_key_schedule schedule;
1130 des_cblock StdText = "KGS!@#$%";
1131 des_cblock hash1, hash2;
1135 for( i=0; i<passwd->bv_len; i++) {
1136 if(passwd->bv_val[i] == '\0') {
1137 return NULL; /* NUL character in password */
1141 if( passwd->bv_val[i] != '\0' ) {
1142 return NULL; /* passwd must behave like a string */
1145 strncpy( UcasePassword, passwd->bv_val, 14 );
1146 UcasePassword[14] = '\0';
1147 ldap_pvt_str2upper( UcasePassword );
1149 lmPasswd_to_key( UcasePassword, &key );
1150 des_set_key_unchecked( &key, schedule );
1151 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1153 lmPasswd_to_key( &UcasePassword[7], &key );
1154 des_set_key_unchecked( &key, schedule );
1155 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1157 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1158 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1159 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1161 hash.bv_val = lmhash;
1164 return pw_string( scheme, &hash );
1166 #endif /* SLAPD_LMHASH */
1169 static struct berval *hash_crypt(
1170 const struct pw_scheme *scheme,
1171 const struct berval *passwd )
1174 unsigned char salt[3];
1177 for( i=0; i<passwd->bv_len; i++) {
1178 if(passwd->bv_val[i] == '\0') {
1179 return NULL; /* NUL character in password */
1183 if( passwd->bv_val[i] != '\0' ) {
1184 return NULL; /* passwd must behave like a string */
1187 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1191 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1192 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1195 hash.bv_val = crypt( passwd->bv_val, salt );
1197 if( hash.bv_val == NULL ) return NULL;
1199 hash.bv_len = strlen( hash.bv_val );
1201 if( hash.bv_len == 0 ) {
1205 return pw_string( scheme, &hash );