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>
24 #include <ac/unistd.h>
27 # include <openssl/des.h>
28 #endif /* SLAPD_LMHASH */
42 # include <ac/crypt.h>
44 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
51 # ifdef HAVE_AIX_SECURITY
61 #include "lutil_md5.h"
62 #include "lutil_sha1.h"
65 static const unsigned char crypt64[] =
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
69 static char *salt_format = NULL;
74 typedef int (*PASSWD_CHK_FUNC)(
75 const struct pw_scheme *scheme,
76 const struct berval *passwd,
77 const struct berval *cred );
79 typedef struct berval * (*PASSWD_HASH_FUNC) (
80 const struct pw_scheme *scheme,
81 const struct berval *passwd );
85 PASSWD_CHK_FUNC chk_fn;
86 PASSWD_HASH_FUNC hash_fn;
89 /* password check routines */
91 const struct pw_scheme *scheme,
92 const struct berval *passwd,
93 const struct berval *cred );
96 const struct pw_scheme *scheme,
97 const struct berval *passwd,
98 const struct berval *cred );
100 #ifdef LUTIL_SHA1_BYTES
101 static int chk_ssha1(
102 const struct pw_scheme *scheme,
103 const struct berval *passwd,
104 const struct berval *cred );
107 const struct pw_scheme *scheme,
108 const struct berval *passwd,
109 const struct berval *cred );
113 static int chk_lanman(
114 const struct pw_scheme *scheme,
115 const struct berval *passwd,
116 const struct berval *cred );
121 const struct pw_scheme *scheme,
122 const struct berval *passwd,
123 const struct berval *cred );
127 static int chk_kerberos(
128 const struct pw_scheme *scheme,
129 const struct berval *passwd,
130 const struct berval *cred );
134 static int chk_crypt(
135 const struct pw_scheme *scheme,
136 const struct berval *passwd,
137 const struct berval *cred );
139 #if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
141 const struct pw_scheme *scheme,
142 const struct berval *passwd,
143 const struct berval *cred );
148 #ifdef LUTIL_SHA1_BYTES
149 /* password hash routines */
150 static struct berval *hash_sha1(
151 const struct pw_scheme *scheme,
152 const struct berval *passwd );
154 static struct berval *hash_ssha1(
155 const struct pw_scheme *scheme,
156 const struct berval *passwd );
159 static struct berval *hash_smd5(
160 const struct pw_scheme *scheme,
161 const struct berval *passwd );
163 static struct berval *hash_md5(
164 const struct pw_scheme *scheme,
165 const struct berval *passwd );
168 static struct berval *hash_lanman(
169 const struct pw_scheme *scheme,
170 const struct berval *passwd );
174 static struct berval *hash_crypt(
175 const struct pw_scheme *scheme,
176 const struct berval *passwd );
180 static const struct pw_scheme pw_schemes[] =
182 #ifdef LUTIL_SHA1_BYTES
183 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
184 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
187 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
188 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
191 { {sizeof("{LANMAN}")-1, "{LANMAN}"}, chk_lanman, hash_lanman },
192 #endif /* SLAPD_LMHASH */
195 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
199 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
203 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
204 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
205 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
209 #ifdef SLAPD_CLEARTEXT
211 { {0, "{CLEARTEXT}"}, NULL, NULL },
214 { {0, NULL}, NULL, NULL }
217 static const struct pw_scheme *get_scheme(
222 for( i=0; pw_schemes[i].name.bv_val; i++) {
223 if( pw_schemes[i].name.bv_len == 0 ) continue;
225 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
226 pw_schemes[i].name.bv_len) == 0 )
228 return &pw_schemes[i];
235 int lutil_passwd_scheme(
238 if( scheme == NULL ) {
242 return get_scheme(scheme) != NULL;
246 static int is_allowed_scheme(
248 const char** schemes )
252 if( schemes == NULL ) return 1;
254 for( i=0; schemes[i] != NULL; i++ ) {
255 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
262 static struct berval *passwd_scheme(
263 const struct pw_scheme *scheme,
264 const struct berval * passwd,
265 const char** allowed )
267 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
271 if( passwd->bv_len >= scheme->name.bv_len ) {
272 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
273 struct berval *bv = ber_memalloc( sizeof(struct berval) );
275 if( bv == NULL ) return NULL;
277 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
278 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
288 * Return 0 if creds are good.
292 const struct berval *passwd, /* stored passwd */
293 const struct berval *cred, /* user cred */
294 const char **schemes )
298 if (cred == NULL || cred->bv_len == 0 ||
299 passwd == NULL || passwd->bv_len == 0 )
304 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
305 if( pw_schemes[i].chk_fn ) {
306 struct berval *p = passwd_scheme( &pw_schemes[i],
310 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
312 /* only free the berval structure as the bv_val points
313 * into passwd->bv_val
322 #ifdef SLAPD_CLEARTEXT
323 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
324 return passwd->bv_len == cred->bv_len
325 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
332 struct berval * lutil_passwd_generate( ber_len_t len )
336 if( len < 1 ) return NULL;
338 pw = ber_memalloc( sizeof( struct berval ) );
339 if( pw == NULL ) return NULL;
342 pw->bv_val = ber_memalloc( len + 1 );
344 if( pw->bv_val == NULL ) {
349 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
354 for( len = 0; len < pw->bv_len; len++ ) {
355 pw->bv_val[len] = crypt64[
356 pw->bv_val[len] % (sizeof(crypt64)-1) ];
359 pw->bv_val[len] = '\0';
364 struct berval * lutil_passwd_hash(
365 const struct berval * passwd,
366 const char * method )
368 const struct pw_scheme *sc = get_scheme( method );
370 if( sc == NULL ) return NULL;
371 if( ! sc->hash_fn ) return NULL;
373 return (sc->hash_fn)( sc, passwd );
376 static struct berval * pw_string(
377 const struct pw_scheme *sc,
378 const struct berval *passwd )
380 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
381 if( pw == NULL ) return NULL;
383 pw->bv_len = sc->name.bv_len + passwd->bv_len;
384 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
386 if( pw->bv_val == NULL ) {
391 AC_MEMCPY( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
392 AC_MEMCPY( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
394 pw->bv_val[pw->bv_len] = '\0';
398 static struct berval * pw_string64(
399 const struct pw_scheme *sc,
400 const struct berval *hash,
401 const struct berval *salt )
404 struct berval string;
405 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
408 if( b64 == NULL ) return NULL;
411 /* need to base64 combined string */
412 string.bv_len = hash->bv_len + salt->bv_len;
413 string.bv_val = ber_memalloc( string.bv_len + 1 );
415 if( string.bv_val == NULL ) {
420 AC_MEMCPY( string.bv_val, hash->bv_val,
422 AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
424 string.bv_val[string.bv_len] = '\0';
430 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
431 b64->bv_len = b64len + sc->name.bv_len;
432 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
434 if( b64->bv_val == NULL ) {
435 if( salt ) ber_memfree( string.bv_val );
440 AC_MEMCPY(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
443 string.bv_val, string.bv_len,
444 &b64->bv_val[sc->name.bv_len], b64len );
446 if( salt ) ber_memfree( string.bv_val );
453 /* recompute length */
454 b64->bv_len = sc->name.bv_len + rc;
455 assert( strlen(b64->bv_val) == b64->bv_len );
459 /* PASSWORD CHECK ROUTINES */
461 #ifdef LUTIL_SHA1_BYTES
462 static int chk_ssha1(
463 const struct pw_scheme *sc,
464 const struct berval * passwd,
465 const struct berval * cred )
467 lutil_SHA1_CTX SHA1context;
468 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
470 unsigned char *orig_pass = NULL;
472 /* decode base64 password */
473 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
474 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
476 if( orig_pass == NULL ) return -1;
478 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
481 ber_memfree(orig_pass);
485 /* hash credentials with salt */
486 lutil_SHA1Init(&SHA1context);
487 lutil_SHA1Update(&SHA1context,
488 (const unsigned char *) cred->bv_val, cred->bv_len);
489 lutil_SHA1Update(&SHA1context,
490 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
491 rc - sizeof(SHA1digest));
492 lutil_SHA1Final(SHA1digest, &SHA1context);
495 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
496 ber_memfree(orig_pass);
501 const struct pw_scheme *sc,
502 const struct berval * passwd,
503 const struct berval * cred )
505 lutil_SHA1_CTX SHA1context;
506 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
508 unsigned char *orig_pass = NULL;
510 /* base64 un-encode password */
511 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
512 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
514 if( orig_pass == NULL ) return -1;
516 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
518 if( rc != sizeof(SHA1digest) ) {
519 ber_memfree(orig_pass);
523 /* hash credentials with salt */
524 lutil_SHA1Init(&SHA1context);
525 lutil_SHA1Update(&SHA1context,
526 (const unsigned char *) cred->bv_val, cred->bv_len);
527 lutil_SHA1Final(SHA1digest, &SHA1context);
530 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
531 ber_memfree(orig_pass);
537 const struct pw_scheme *sc,
538 const struct berval * passwd,
539 const struct berval * cred )
541 lutil_MD5_CTX MD5context;
542 unsigned char MD5digest[LUTIL_MD5_BYTES];
544 unsigned char *orig_pass = NULL;
546 /* base64 un-encode password */
547 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
548 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
550 if( orig_pass == NULL ) return -1;
552 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
554 ber_memfree(orig_pass);
558 /* hash credentials with salt */
559 lutil_MD5Init(&MD5context);
560 lutil_MD5Update(&MD5context,
561 (const unsigned char *) cred->bv_val,
563 lutil_MD5Update(&MD5context,
564 &orig_pass[sizeof(MD5digest)],
565 rc - sizeof(MD5digest));
566 lutil_MD5Final(MD5digest, &MD5context);
569 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
570 ber_memfree(orig_pass);
575 const struct pw_scheme *sc,
576 const struct berval * passwd,
577 const struct berval * cred )
579 lutil_MD5_CTX MD5context;
580 unsigned char MD5digest[LUTIL_MD5_BYTES];
582 unsigned char *orig_pass = NULL;
584 /* base64 un-encode password */
585 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
586 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
588 if( orig_pass == NULL ) return -1;
590 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
591 if ( rc != sizeof(MD5digest) ) {
592 ber_memfree(orig_pass);
596 /* hash credentials with salt */
597 lutil_MD5Init(&MD5context);
598 lutil_MD5Update(&MD5context,
599 (const unsigned char *) cred->bv_val,
601 lutil_MD5Final(MD5digest, &MD5context);
604 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
605 ber_memfree(orig_pass);
610 static int chk_lanman(
611 const struct pw_scheme *scheme,
612 const struct berval *passwd,
613 const struct berval *cred )
617 hash = hash_lanman( scheme, cred );
618 return memcmp( &hash->bv_val[scheme->name.bv_len], passwd->bv_val, 32);
620 #endif /* SLAPD_LMHASH */
623 #ifdef HAVE_CYRUS_SASL
624 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
628 const struct pw_scheme *sc,
629 const struct berval * passwd,
630 const struct berval * cred )
635 for( i=0; i<cred->bv_len; i++) {
636 if(cred->bv_val[i] == '\0') {
637 return 1; /* NUL character in password */
641 if( cred->bv_val[i] != '\0' ) {
642 return 1; /* cred must behave like a string */
645 for( i=0; i<passwd->bv_len; i++) {
646 if(passwd->bv_val[i] == '\0') {
647 return 1; /* NUL character in password */
651 if( passwd->bv_val[i] != '\0' ) {
652 return 1; /* passwd must behave like a string */
657 #ifdef HAVE_CYRUS_SASL
658 if( lutil_passwd_sasl_conn != NULL ) {
659 const char *errstr = NULL;
662 sc = sasl_checkpass( lutil_passwd_sasl_conn,
663 passwd->bv_val, passwd->bv_len,
664 cred->bv_val, cred->bv_len,
667 rtn = ( sc != SASL_OK );
676 static int chk_kerberos(
677 const struct pw_scheme *sc,
678 const struct berval * passwd,
679 const struct berval * cred )
684 for( i=0; i<cred->bv_len; i++) {
685 if(cred->bv_val[i] == '\0') {
686 return 1; /* NUL character in password */
690 if( cred->bv_val[i] != '\0' ) {
691 return 1; /* cred must behave like a string */
694 for( i=0; i<passwd->bv_len; i++) {
695 if(passwd->bv_val[i] == '\0') {
696 return 1; /* NUL character in password */
700 if( passwd->bv_val[i] != '\0' ) {
701 return 1; /* passwd must behave like a string */
706 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
709 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
710 * (Royal Institute of Technology, Stockholm, Sweden).
711 * All rights reserved.
713 * Redistribution and use in source and binary forms, with or without
714 * modification, are permitted provided that the following conditions
717 * 1. Redistributions of source code must retain the above copyright
718 * notice, this list of conditions and the following disclaimer.
720 * 2. Redistributions in binary form must reproduce the above copyright
721 * notice, this list of conditions and the following disclaimer in the
722 * documentation and/or other materials provided with the distribution.
724 * 3. Neither the name of the Institute nor the names of its contributors
725 * may be used to endorse or promote products derived from this software
726 * without specific prior written permission.
728 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
729 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
730 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
731 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
732 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
733 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
734 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
735 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
736 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
737 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
741 krb5_context context;
744 krb5_get_init_creds_opt get_options;
745 krb5_verify_init_creds_opt verify_options;
746 krb5_principal client, server;
748 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
751 ret = krb5_init_context( &context );
757 krb5_get_init_creds_opt_set_preauth_list(&get_options,
761 krb5_get_init_creds_opt_init( &get_options );
763 krb5_verify_init_creds_opt_init( &verify_options );
765 ret = krb5_parse_name( context, passwd->bv_val, &client );
768 krb5_free_context( context );
772 ret = krb5_get_init_creds_password( context,
773 &creds, client, cred->bv_val, NULL,
774 NULL, 0, NULL, &get_options );
777 krb5_free_principal( context, client );
778 krb5_free_context( context );
783 char *host = ldap_pvt_get_fqdn( NULL );
786 krb5_free_principal( context, client );
787 krb5_free_context( context );
791 ret = krb5_sname_to_principal( context,
792 host, "ldap", KRB5_NT_SRV_HST, &server );
798 krb5_free_principal( context, client );
799 krb5_free_context( context );
803 ret = krb5_verify_init_creds( context,
804 &creds, server, NULL, NULL, &verify_options );
806 krb5_free_principal( context, client );
807 krb5_free_principal( context, server );
808 krb5_free_cred_contents( context, &creds );
809 krb5_free_context( context );
813 #elif defined(HAVE_KRB4)
815 /* Borrowed from Heimdal kpopper */
817 * Copyright (c) 1989 Regents of the University of California.
818 * All rights reserved. The Berkeley software License Agreement
819 * specifies the terms and conditions for redistribution.
823 char lrealm[REALM_SZ];
824 char tkt[MAXHOSTNAMELEN];
826 status = krb_get_lrealm(lrealm,1);
827 if (status == KFAILURE) {
831 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
832 TKT_ROOT, (unsigned)getpid());
833 krb_set_tkt_string (tkt);
835 status = krb_verify_user( passwd->bv_val, "", lrealm,
836 cred->bv_val, 1, "ldap");
838 dest_tkt(); /* no point in keeping the tickets */
840 return status == KFAILURE;
846 #endif /* SLAPD_KPASSWD */
849 static int chk_crypt(
850 const struct pw_scheme *sc,
851 const struct berval * passwd,
852 const struct berval * cred )
857 for( i=0; i<cred->bv_len; i++) {
858 if(cred->bv_val[i] == '\0') {
859 return 1; /* NUL character in password */
863 if( cred->bv_val[i] != '\0' ) {
864 return -1; /* cred must behave like a string */
867 if( passwd->bv_len < 2 ) {
868 return -1; /* passwd must be at least two characters long */
871 for( i=0; i<passwd->bv_len; i++) {
872 if(passwd->bv_val[i] == '\0') {
873 return -1; /* NUL character in password */
877 if( passwd->bv_val[i] != '\0' ) {
878 return -1; /* passwd must behave like a string */
881 cr = crypt( cred->bv_val, passwd->bv_val );
883 if( cr == NULL || cr[0] == '\0' ) {
884 /* salt must have been invalid */
888 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
891 # if defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD )
893 const struct pw_scheme *sc,
894 const struct berval * passwd,
895 const struct berval * cred )
900 for( i=0; i<cred->bv_len; i++) {
901 if(cred->bv_val[i] == '\0') {
902 return -1; /* NUL character in password */
905 if( cred->bv_val[i] != '\0' ) {
906 return -1; /* cred must behave like a string */
909 for( i=0; i<passwd->bv_len; i++) {
910 if(passwd->bv_val[i] == '\0') {
911 return -1; /* NUL character in password */
915 if( passwd->bv_val[i] != '\0' ) {
916 return -1; /* passwd must behave like a string */
920 struct passwd *pwd = getpwnam(passwd->bv_val);
923 return -1; /* not found */
928 # ifdef HAVE_GETSPNAM
930 struct spwd *spwd = getspnam(passwd->bv_val);
937 # ifdef HAVE_AIX_SECURITY
939 struct userpw *upw = getuserpw(passwd->bv_val);
942 pw = upw->upw_passwd;
947 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
948 /* password must must be at least two characters long */
952 cr = crypt(cred->bv_val, pw);
954 if( cr == NULL || cr[0] == '\0' ) {
955 /* salt must have been invalid */
959 return strcmp(pw, cr) ? 1 : 0;
965 /* PASSWORD GENERATION ROUTINES */
967 #ifdef LUTIL_SHA1_BYTES
968 static struct berval *hash_ssha1(
969 const struct pw_scheme *scheme,
970 const struct berval *passwd )
972 lutil_SHA1_CTX SHA1context;
973 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
974 unsigned char saltdata[4];
975 struct berval digest;
978 digest.bv_val = SHA1digest;
979 digest.bv_len = sizeof(SHA1digest);
980 salt.bv_val = saltdata;
981 salt.bv_len = sizeof(saltdata);
983 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
987 lutil_SHA1Init( &SHA1context );
988 lutil_SHA1Update( &SHA1context,
989 (const unsigned char *)passwd->bv_val, passwd->bv_len );
990 lutil_SHA1Update( &SHA1context,
991 (const unsigned char *)salt.bv_val, salt.bv_len );
992 lutil_SHA1Final( SHA1digest, &SHA1context );
994 return pw_string64( scheme, &digest, &salt);
997 static struct berval *hash_sha1(
998 const struct pw_scheme *scheme,
999 const struct berval *passwd )
1001 lutil_SHA1_CTX SHA1context;
1002 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
1003 struct berval digest;
1004 digest.bv_val = SHA1digest;
1005 digest.bv_len = sizeof(SHA1digest);
1007 lutil_SHA1Init( &SHA1context );
1008 lutil_SHA1Update( &SHA1context,
1009 (const unsigned char *)passwd->bv_val, passwd->bv_len );
1010 lutil_SHA1Final( SHA1digest, &SHA1context );
1012 return pw_string64( scheme, &digest, NULL);
1016 static struct berval *hash_smd5(
1017 const struct pw_scheme *scheme,
1018 const struct berval *passwd )
1020 lutil_MD5_CTX MD5context;
1021 unsigned char MD5digest[LUTIL_MD5_BYTES];
1022 unsigned char saltdata[4];
1023 struct berval digest;
1026 digest.bv_val = MD5digest;
1027 digest.bv_len = sizeof(MD5digest);
1028 salt.bv_val = saltdata;
1029 salt.bv_len = sizeof(saltdata);
1031 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
1035 lutil_MD5Init( &MD5context );
1036 lutil_MD5Update( &MD5context,
1037 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1038 lutil_MD5Update( &MD5context,
1039 (const unsigned char *) salt.bv_val, salt.bv_len );
1040 lutil_MD5Final( MD5digest, &MD5context );
1042 return pw_string64( scheme, &digest, &salt );
1045 static struct berval *hash_md5(
1046 const struct pw_scheme *scheme,
1047 const struct berval *passwd )
1049 lutil_MD5_CTX MD5context;
1050 unsigned char MD5digest[LUTIL_MD5_BYTES];
1052 struct berval digest;
1054 digest.bv_val = MD5digest;
1055 digest.bv_len = sizeof(MD5digest);
1057 lutil_MD5Init( &MD5context );
1058 lutil_MD5Update( &MD5context,
1059 (const unsigned char *) passwd->bv_val, passwd->bv_len );
1060 lutil_MD5Final( MD5digest, &MD5context );
1062 return pw_string64( scheme, &digest, NULL );
1067 /* pseudocode from RFC2433
1068 * A.2 LmPasswordHash()
1071 * IN 0-to-14-oem-char Password,
1072 * OUT 16-octet PasswordHash )
1074 * Set UcasePassword to the uppercased Password
1075 * Zero pad UcasePassword to 14 characters
1077 * DesHash( 1st 7-octets of UcasePassword,
1078 * giving 1st 8-octets of PasswordHash )
1080 * DesHash( 2nd 7-octets of UcasePassword,
1081 * giving 2nd 8-octets of PasswordHash )
1089 * OUT 8-octet Cypher )
1092 * * Make Cypher an irreversibly encrypted form of Clear by
1093 * * encrypting known text using Clear as the secret key.
1094 * * The known text consists of the string
1099 * Set StdText to "KGS!@#$%"
1100 * DesEncrypt( StdText, Clear, giving Cypher )
1109 * OUT 8-octet Cypher )
1112 * * Use the DES encryption algorithm [4] in ECB mode [9]
1113 * * to encrypt Clear into Cypher such that Cypher can
1114 * * only be decrypted back to Clear by providing Key.
1115 * * Note that the DES algorithm takes as input a 64-bit
1116 * * stream where the 8th, 16th, 24th, etc. bits are
1117 * * parity bits ignored by the encrypting algorithm.
1118 * * Unless you write your own DES to accept 56-bit input
1119 * * without parity, you will need to insert the parity bits
1125 static void lmPasswd_to_key(
1126 const unsigned char *lmPasswd,
1129 /* make room for parity bits */
1130 ((char *)key)[0] = lmPasswd[0];
1131 ((char *)key)[1] = ((lmPasswd[0]&0x01)<<7) | (lmPasswd[1]>>1);
1132 ((char *)key)[2] = ((lmPasswd[1]&0x03)<<6) | (lmPasswd[2]>>2);
1133 ((char *)key)[3] = ((lmPasswd[2]&0x07)<<5) | (lmPasswd[3]>>3);
1134 ((char *)key)[4] = ((lmPasswd[3]&0x0F)<<4) | (lmPasswd[4]>>4);
1135 ((char *)key)[5] = ((lmPasswd[4]&0x1F)<<3) | (lmPasswd[5]>>5);
1136 ((char *)key)[6] = ((lmPasswd[5]&0x3F)<<2) | (lmPasswd[6]>>6);
1137 ((char *)key)[7] = ((lmPasswd[6]&0x7F)<<1);
1139 des_set_odd_parity( key );
1142 static struct berval *hash_lanman(
1143 const struct pw_scheme *scheme,
1144 const struct berval *passwd )
1148 char UcasePassword[15];
1150 des_key_schedule schedule;
1151 des_cblock StdText = "KGS!@#$%";
1152 des_cblock hash1, hash2;
1156 for( i=0; i<passwd->bv_len; i++) {
1157 if(passwd->bv_val[i] == '\0') {
1158 return NULL; /* NUL character in password */
1162 if( passwd->bv_val[i] != '\0' ) {
1163 return NULL; /* passwd must behave like a string */
1166 strncpy( UcasePassword, passwd->bv_val, 14 );
1167 UcasePassword[14] = '\0';
1168 ldap_pvt_str2upper( UcasePassword );
1170 lmPasswd_to_key( UcasePassword, &key );
1171 des_set_key_unchecked( &key, schedule );
1172 des_ecb_encrypt( &StdText, &hash1, schedule , DES_ENCRYPT );
1174 lmPasswd_to_key( &UcasePassword[7], &key );
1175 des_set_key_unchecked( &key, schedule );
1176 des_ecb_encrypt( &StdText, &hash2, schedule , DES_ENCRYPT );
1178 sprintf( lmhash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1179 hash1[0],hash1[1],hash1[2],hash1[3],hash1[4],hash1[5],hash1[6],hash1[7],
1180 hash2[0],hash2[1],hash2[2],hash2[3],hash2[4],hash2[5],hash2[6],hash2[7] );
1182 hash.bv_val = lmhash;
1185 return pw_string( scheme, &hash );
1187 #endif /* SLAPD_LMHASH */
1190 static struct berval *hash_crypt(
1191 const struct pw_scheme *scheme,
1192 const struct berval *passwd )
1195 unsigned char salt[32]; /* salt suitable for most anything */
1198 for( i=0; i<passwd->bv_len; i++) {
1199 if(passwd->bv_val[i] == '\0') {
1200 return NULL; /* NUL character in password */
1204 if( passwd->bv_val[i] != '\0' ) {
1205 return NULL; /* passwd must behave like a string */
1208 if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
1212 for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
1213 salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
1215 salt[sizeof( salt ) - 1 ] = '\0';
1217 if( salt_format != NULL ) {
1218 /* copy the salt we made into entropy before snprintfing
1219 it back into the salt */
1220 char entropy[sizeof(salt)];
1221 strcpy( entropy, salt );
1222 snprintf( salt, sizeof(entropy), salt_format, entropy );
1225 hash.bv_val = crypt( passwd->bv_val, salt );
1227 if( hash.bv_val == NULL ) return NULL;
1229 hash.bv_len = strlen( hash.bv_val );
1231 if( hash.bv_len == 0 ) {
1235 return pw_string( scheme, &hash );
1239 int lutil_salt_format(const char *format)
1242 free( salt_format );
1244 salt_format = format != NULL ? strdup( format ) : NULL;