3 * Copyright 1998-1999 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>
36 #include <ac/unistd.h>
48 #include "lutil_md5.h"
49 #include "lutil_sha1.h"
52 static const unsigned char crypt64[] =
53 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
57 typedef int (*PASSWD_CHK_FUNC)(
58 const struct pw_scheme *scheme,
59 const struct berval *passwd,
60 const struct berval *cred );
62 typedef struct berval * (*PASSWD_HASH_FUNC) (
63 const struct pw_scheme *scheme,
64 const struct berval *passwd );
68 PASSWD_CHK_FUNC chk_fn;
69 PASSWD_HASH_FUNC hash_fn;
72 /* password check routines */
74 const struct pw_scheme *scheme,
75 const struct berval *passwd,
76 const struct berval *cred );
79 const struct pw_scheme *scheme,
80 const struct berval *passwd,
81 const struct berval *cred );
84 const struct pw_scheme *scheme,
85 const struct berval *passwd,
86 const struct berval *cred );
89 const struct pw_scheme *scheme,
90 const struct berval *passwd,
91 const struct berval *cred );
95 const struct pw_scheme *scheme,
96 const struct berval *passwd,
97 const struct berval *cred );
101 static int chk_kerberos(
102 const struct pw_scheme *scheme,
103 const struct berval *passwd,
104 const struct berval *cred );
107 static int chk_crypt(
108 const struct pw_scheme *scheme,
109 const struct berval *passwd,
110 const struct berval *cred );
113 const struct pw_scheme *scheme,
114 const struct berval *passwd,
115 const struct berval *cred );
118 /* password hash routines */
119 static struct berval *hash_sha1(
120 const struct pw_scheme *scheme,
121 const struct berval *passwd );
123 static struct berval *hash_ssha1(
124 const struct pw_scheme *scheme,
125 const struct berval *passwd );
127 static struct berval *hash_smd5(
128 const struct pw_scheme *scheme,
129 const struct berval *passwd );
131 static struct berval *hash_md5(
132 const struct pw_scheme *scheme,
133 const struct berval *passwd );
135 static struct berval *hash_crypt(
136 const struct pw_scheme *scheme,
137 const struct berval *passwd );
140 static const struct pw_scheme pw_schemes[] =
142 { {sizeof("{SSHA}")-1, "{SSHA}"}, chk_ssha1, hash_ssha1 },
143 { {sizeof("{SHA}")-1, "{SHA}"}, chk_sha1, hash_sha1 },
145 { {sizeof("{SMD5}")-1, "{SMD5}"}, chk_smd5, hash_smd5 },
146 { {sizeof("{MD5}")-1, "{MD5}"}, chk_md5, hash_md5 },
149 { {sizeof("{SASL}")-1, "{SASL}"}, chk_sasl, NULL },
153 { {sizeof("{KERBEROS}")-1, "{KERBEROS}"}, chk_kerberos, NULL },
157 { {sizeof("{CRYPT}")-1, "{CRYPT}"}, chk_crypt, hash_crypt },
159 # if defined( HAVE_GETSPNAM ) \
160 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
161 { {sizeof("{UNIX}")-1, "{UNIX}"}, chk_unix, NULL },
164 #ifdef SLAPD_CLEARTEXT
166 { {0, "{CLEARTEXT}"}, NULL, NULL },
169 { {0, NULL}, NULL, NULL }
172 static const struct pw_scheme *get_scheme(
177 for( i=0; pw_schemes[i].name.bv_val; i++) {
178 if( pw_schemes[i].name.bv_len == 0 ) continue;
180 if( strncasecmp(scheme, pw_schemes[i].name.bv_val,
181 pw_schemes[i].name.bv_len) == 0 )
183 return &pw_schemes[i];
190 int lutil_passwd_scheme(
193 if( scheme == NULL ) {
197 return get_scheme(scheme) != NULL;
201 static int is_allowed_scheme(
203 const char** schemes )
207 if( schemes == NULL ) return 1;
209 for( i=0; schemes[i] != NULL; i++ ) {
210 if( strcasecmp( scheme, schemes[i] ) == 0 ) {
217 static struct berval *passwd_scheme(
218 const struct pw_scheme *scheme,
219 const struct berval * passwd,
220 const char** allowed )
222 if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
226 if( passwd->bv_len >= scheme->name.bv_len ) {
227 if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
228 struct berval *bv = ber_memalloc( sizeof(struct berval) );
230 if( bv == NULL ) return NULL;
232 bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
233 bv->bv_len = passwd->bv_len - scheme->name.bv_len;
243 * Return 0 if creds are good.
247 const struct berval *passwd, /* stored passwd */
248 const struct berval *cred, /* user cred */
249 const char **schemes )
253 if (cred == NULL || cred->bv_len == 0 ||
254 passwd == NULL || passwd->bv_len == 0 )
259 for( i=0; pw_schemes[i].name.bv_val != NULL; i++ ) {
260 if( pw_schemes[i].chk_fn ) {
261 struct berval *p = passwd_scheme( &pw_schemes[i],
265 int rc = (pw_schemes[i].chk_fn)( &pw_schemes[i], p, cred );
267 /* only free the berval structure as the bv_val points
268 * into passwd->bv_val
277 #ifdef SLAPD_CLEARTEXT
278 if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
279 return passwd->bv_len == cred->bv_len
280 ? memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
289 struct berval * lutil_passwd_generate( ber_len_t len )
293 if( len < 1 ) return NULL;
295 pw = ber_memalloc( sizeof( struct berval ) );
296 if( pw == NULL ) return NULL;
299 pw->bv_val = ber_memalloc( len + 1 );
301 if( pw->bv_val == NULL ) {
306 if( lutil_entropy( pw->bv_val, pw->bv_len) < 0 ) {
311 for( len = 0; len < pw->bv_len; len++ ) {
312 pw->bv_val[len] = crypt64[
313 pw->bv_val[len] % (sizeof(crypt64)-1) ];
316 pw->bv_val[len] = '\0';
321 struct berval * lutil_passwd_hash(
322 const struct berval * passwd,
323 const char * method )
325 const struct pw_scheme *sc = get_scheme( method );
327 if( sc == NULL ) return NULL;
328 if( ! sc->hash_fn ) return NULL;
330 return (sc->hash_fn)( sc, passwd );
333 static struct berval * pw_string(
334 const struct pw_scheme *sc,
335 const struct berval *passwd )
337 struct berval *pw = ber_memalloc( sizeof( struct berval ) );
338 if( pw == NULL ) return NULL;
340 pw->bv_len = sc->name.bv_len + passwd->bv_len;
341 pw->bv_val = ber_memalloc( pw->bv_len + 1 );
343 if( pw->bv_val == NULL ) {
348 memcpy( pw->bv_val, sc->name.bv_val, sc->name.bv_len );
349 memcpy( &pw->bv_val[sc->name.bv_len], passwd->bv_val, passwd->bv_len );
351 pw->bv_val[pw->bv_len] = '\0';
355 static struct berval * pw_string64(
356 const struct pw_scheme *sc,
357 const struct berval *hash,
358 const struct berval *salt )
361 struct berval string;
362 struct berval *b64 = ber_memalloc( sizeof(struct berval) );
365 if( b64 == NULL ) return NULL;
368 /* need to base64 combined string */
369 string.bv_len = hash->bv_len + salt->bv_len;
370 string.bv_val = ber_memalloc( string.bv_len + 1 );
372 if( string.bv_val == NULL ) {
377 memcpy( string.bv_val, hash->bv_val,
379 memcpy( &string.bv_val[hash->bv_len], salt->bv_val,
381 string.bv_val[string.bv_len] = '\0';
387 b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
388 b64->bv_len = b64len + sc->name.bv_len;
389 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
391 if( b64->bv_val == NULL ) {
392 if( salt ) ber_memfree( string.bv_val );
397 memcpy(b64->bv_val, sc->name.bv_val, sc->name.bv_len);
400 string.bv_val, string.bv_len,
401 &b64->bv_val[sc->name.bv_len], b64len );
403 if( salt ) ber_memfree( string.bv_val );
410 /* recompute length */
411 b64->bv_len = sc->name.bv_len + rc;
412 assert( strlen(b64->bv_val) == b64->bv_len );
416 /* PASSWORD CHECK ROUTINES */
418 static int chk_ssha1(
419 const struct pw_scheme *sc,
420 const struct berval * passwd,
421 const struct berval * cred )
423 lutil_SHA1_CTX SHA1context;
424 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
426 unsigned char *orig_pass = NULL;
428 /* decode base64 password */
429 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
430 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
432 if( orig_pass == NULL ) return -1;
434 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
437 ber_memfree(orig_pass);
441 /* hash credentials with salt */
442 lutil_SHA1Init(&SHA1context);
443 lutil_SHA1Update(&SHA1context,
444 (const unsigned char *) cred->bv_val, cred->bv_len);
445 lutil_SHA1Update(&SHA1context,
446 (const unsigned char *) &orig_pass[sizeof(SHA1digest)],
447 rc - sizeof(SHA1digest));
448 lutil_SHA1Final(SHA1digest, &SHA1context);
451 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
452 ber_memfree(orig_pass);
457 const struct pw_scheme *sc,
458 const struct berval * passwd,
459 const struct berval * cred )
461 lutil_SHA1_CTX SHA1context;
462 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
464 unsigned char *orig_pass = NULL;
466 /* base64 un-encode password */
467 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
468 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
470 if( orig_pass == NULL ) return -1;
472 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
474 if( rc != sizeof(SHA1digest) ) {
475 ber_memfree(orig_pass);
479 /* hash credentials with salt */
480 lutil_SHA1Init(&SHA1context);
481 lutil_SHA1Update(&SHA1context,
482 (const unsigned char *) cred->bv_val, cred->bv_len);
483 lutil_SHA1Final(SHA1digest, &SHA1context);
486 rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
487 ber_memfree(orig_pass);
492 const struct pw_scheme *sc,
493 const struct berval * passwd,
494 const struct berval * cred )
496 lutil_MD5_CTX MD5context;
497 unsigned char MD5digest[LUTIL_MD5_BYTES];
499 unsigned char *orig_pass = NULL;
501 /* base64 un-encode password */
502 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
503 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
505 if( orig_pass == NULL ) return -1;
507 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
509 ber_memfree(orig_pass);
513 /* hash credentials with salt */
514 lutil_MD5Init(&MD5context);
515 lutil_MD5Update(&MD5context,
516 (const unsigned char *) cred->bv_val, cred->bv_len );
517 lutil_MD5Update(&MD5context,
518 (const unsigned char *) &orig_pass[sizeof(MD5digest)],
519 rc - sizeof(MD5digest));
520 lutil_MD5Final(MD5digest, &MD5context);
523 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
524 ber_memfree(orig_pass);
529 const struct pw_scheme *sc,
530 const struct berval * passwd,
531 const struct berval * cred )
533 lutil_MD5_CTX MD5context;
534 unsigned char MD5digest[LUTIL_MD5_BYTES];
536 unsigned char *orig_pass = NULL;
538 /* base64 un-encode password */
539 orig_pass = (unsigned char *) ber_memalloc( (size_t) (
540 LUTIL_BASE64_DECODE_LEN(passwd->bv_len) + 1) );
542 if( orig_pass == NULL ) return -1;
544 rc = lutil_b64_pton(passwd->bv_val, orig_pass, passwd->bv_len);
545 if ( rc != sizeof(MD5digest) ) {
546 ber_memfree(orig_pass);
550 /* hash credentials with salt */
551 lutil_MD5Init(&MD5context);
552 lutil_MD5Update(&MD5context,
553 (const unsigned char *) cred->bv_val, cred->bv_len );
554 lutil_MD5Final(MD5digest, &MD5context);
557 rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
558 ber_memfree(orig_pass);
563 #ifdef HAVE_CYRUS_SASL
564 sasl_conn_t *lutil_passwd_sasl_conn = NULL;
568 const struct pw_scheme *sc,
569 const struct berval * passwd,
570 const struct berval * cred )
575 for( i=0; i<cred->bv_len; i++) {
576 if(cred->bv_val[i] == '\0') {
577 return 1; /* NUL character in password */
581 if( cred->bv_val[i] != '\0' ) {
582 return 1; /* cred must behave like a string */
585 for( i=0; i<passwd->bv_len; i++) {
586 if(passwd->bv_val[i] == '\0') {
587 return 1; /* NUL character in password */
591 if( passwd->bv_val[i] != '\0' ) {
592 return 1; /* passwd must behave like a string */
597 #ifdef HAVE_CYRUS_SASL
598 if( lutil_passwd_sasl_conn != NULL ) {
599 const char *errstr = NULL;
602 sc = sasl_checkpass( lutil_passwd_sasl_conn,
603 passwd->bv_val, passwd->bv_len,
604 cred->bv_val, cred->bv_len,
607 rtn = ( sc != SASL_OK );
616 static int chk_kerberos(
617 const struct pw_scheme *sc,
618 const struct berval * passwd,
619 const struct berval * cred )
624 for( i=0; i<cred->bv_len; i++) {
625 if(cred->bv_val[i] == '\0') {
626 return 1; /* NUL character in password */
630 if( cred->bv_val[i] != '\0' ) {
631 return 1; /* cred must behave like a string */
634 for( i=0; i<passwd->bv_len; i++) {
635 if(passwd->bv_val[i] == '\0') {
636 return 1; /* NUL character in password */
640 if( passwd->bv_val[i] != '\0' ) {
641 return 1; /* passwd must behave like a string */
646 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
649 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
650 * (Royal Institute of Technology, Stockholm, Sweden).
651 * All rights reserved.
653 * Redistribution and use in source and binary forms, with or without
654 * modification, are permitted provided that the following conditions
657 * 1. Redistributions of source code must retain the above copyright
658 * notice, this list of conditions and the following disclaimer.
660 * 2. Redistributions in binary form must reproduce the above copyright
661 * notice, this list of conditions and the following disclaimer in the
662 * documentation and/or other materials provided with the distribution.
664 * 3. Neither the name of the Institute nor the names of its contributors
665 * may be used to endorse or promote products derived from this software
666 * without specific prior written permission.
668 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
669 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
670 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
671 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
672 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
673 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
674 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
675 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
676 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
677 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
681 krb5_context context;
684 krb5_get_init_creds_opt get_options;
685 krb5_verify_init_creds_opt verify_options;
686 krb5_principal client, server;
688 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
691 ret = krb5_init_context( &context );
697 krb5_get_init_creds_opt_set_preauth_list(&get_options,
701 krb5_get_init_creds_opt_init( &get_options );
703 krb5_verify_init_creds_opt_init( &verify_options );
705 ret = krb5_parse_name( context, passwd->bv_val, &client );
708 krb5_free_context( context );
712 ret = krb5_get_init_creds_password( context,
713 &creds, client, cred->bv_val, NULL,
714 NULL, 0, NULL, &get_options );
717 krb5_free_principal( context, client );
718 krb5_free_context( context );
723 char host[MAXHOSTNAMELEN];
725 if( gethostname( host, MAXHOSTNAMELEN ) != 0 ) {
726 krb5_free_principal( context, client );
727 krb5_free_context( context );
731 ret = krb5_sname_to_principal( context,
732 host, "ldap", KRB5_NT_SRV_HST, &server );
736 krb5_free_principal( context, client );
737 krb5_free_context( context );
741 ret = krb5_verify_init_creds( context,
742 &creds, server, NULL, NULL, &verify_options );
744 krb5_free_principal( context, client );
745 krb5_free_principal( context, server );
746 krb5_free_creds_contents( context, &creds );
747 krb5_free_context( context );
751 #elif defined(HAVE_KRB4)
753 /* Borrowed from Heimdal kpopper */
755 * Copyright (c) 1989 Regents of the University of California.
756 * All rights reserved. The Berkeley software License Agreement
757 * specifies the terms and conditions for redistribution.
761 char lrealm[REALM_SZ];
762 char tkt[MAXHOSTNAMELEN];
764 status = krb_get_lrealm(lrealm,1);
765 if (status == KFAILURE) {
769 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
770 TKT_ROOT, (unsigned)getpid());
771 krb_set_tkt_string (tkt);
773 status = krb_verify_user( passwd->bv_val, "", lrealm,
774 cred->bv_val, 1, "ldap");
776 dest_tkt(); /* no point in keeping the tickets */
778 return status == KFAILURE;
784 #endif /* SLAPD_KPASSWD */
787 static int chk_crypt(
788 const struct pw_scheme *sc,
789 const struct berval * passwd,
790 const struct berval * cred )
795 for( i=0; i<cred->bv_len; i++) {
796 if(cred->bv_val[i] == '\0') {
797 return 1; /* NUL character in password */
801 if( cred->bv_val[i] != '\0' ) {
802 return -1; /* cred must behave like a string */
805 if( passwd->bv_len < 2 ) {
806 return -1; /* passwd must be at least two characters long */
809 for( i=0; i<passwd->bv_len; i++) {
810 if(passwd->bv_val[i] == '\0') {
811 return -1; /* NUL character in password */
815 if( passwd->bv_val[i] != '\0' ) {
816 return -1; /* passwd must behave like a string */
819 cr = crypt( cred->bv_val, passwd->bv_val );
821 if( cr == NULL || cr[0] == '\0' ) {
822 /* salt must have been invalid */
826 return strcmp( passwd->bv_val, cr ) ? 1 : 0;
829 # if defined( HAVE_GETSPNAM ) \
830 || ( defined( HAVE_GETPWNAM ) && defined( HAVE_PW_PASSWD ) )
832 const struct pw_scheme *sc,
833 const struct berval * passwd,
834 const struct berval * cred )
839 for( i=0; i<cred->bv_len; i++) {
840 if(cred->bv_val[i] == '\0') {
841 return -1; /* NUL character in password */
844 if( cred->bv_val[i] != '\0' ) {
845 return -1; /* cred must behave like a string */
848 for( i=0; i<passwd->bv_len; i++) {
849 if(passwd->bv_val[i] == '\0') {
850 return -1; /* NUL character in password */
854 if( passwd->bv_val[i] != '\0' ) {
855 return -1; /* passwd must behave like a string */
858 # ifdef HAVE_GETSPNAM
860 struct spwd *spwd = getspnam(passwd->bv_val);
863 return -1; /* not found */
871 struct passwd *pwd = getpwnam(passwd->bv_val);
874 return -1; /* not found */
881 if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
882 /* password must must be at least two characters long */
886 cr = crypt(cred->bv_val, pw);
888 if( cr == NULL || cr[0] == '\0' ) {
889 /* salt must have been invalid */
893 return strcmp(pw, cr) ? 1 : 0;
899 /* PASSWORD GENERATION ROUTINES */
901 static struct berval *hash_ssha1(
902 const struct pw_scheme *scheme,
903 const struct berval *passwd )
905 lutil_SHA1_CTX SHA1context;
906 unsigned char SHA1digest[LUTIL_SHA1_BYTES];
907 unsigned char saltdata[4];
908 struct berval digest;
911 digest.bv_val = SHA1digest;
912 digest.bv_len = sizeof(SHA1digest);
913 salt.bv_val = saltdata;
914 salt.bv_len = sizeof(saltdata);
916 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
920 lutil_SHA1Init( &SHA1context );
921 lutil_SHA1Update( &SHA1context,
922 (const unsigned char *)passwd->bv_val, passwd->bv_len );
923 lutil_SHA1Update( &SHA1context,
924 (const unsigned char *)salt.bv_val, salt.bv_len );
925 lutil_SHA1Final( SHA1digest, &SHA1context );
927 return pw_string64( scheme, &digest, &salt);
930 static struct berval *hash_sha1(
931 const struct pw_scheme *scheme,
932 const struct berval *passwd )
934 lutil_SHA1_CTX SHA1context;
935 unsigned char SHA1digest[20];
936 struct berval digest;
937 digest.bv_val = SHA1digest;
938 digest.bv_len = sizeof(SHA1digest);
940 lutil_SHA1Init( &SHA1context );
941 lutil_SHA1Update( &SHA1context,
942 (const unsigned char *)passwd->bv_val, passwd->bv_len );
943 lutil_SHA1Final( SHA1digest, &SHA1context );
945 return pw_string64( scheme, &digest, NULL);
948 static struct berval *hash_smd5(
949 const struct pw_scheme *scheme,
950 const struct berval *passwd )
952 lutil_MD5_CTX MD5context;
953 unsigned char MD5digest[16];
954 unsigned char saltdata[4];
955 struct berval digest;
958 digest.bv_val = MD5digest;
959 digest.bv_len = sizeof(MD5digest);
960 salt.bv_val = saltdata;
961 salt.bv_len = sizeof(saltdata);
963 if( lutil_entropy( salt.bv_val, salt.bv_len) < 0 ) {
967 lutil_MD5Init( &MD5context );
968 lutil_MD5Update( &MD5context,
969 (const unsigned char *) passwd->bv_val, passwd->bv_len );
970 lutil_MD5Update( &MD5context,
971 (const unsigned char *) salt.bv_val, salt.bv_len );
972 lutil_MD5Final( MD5digest, &MD5context );
974 return pw_string64( scheme, &digest, &salt );
977 static struct berval *hash_md5(
978 const struct pw_scheme *scheme,
979 const struct berval *passwd )
981 lutil_MD5_CTX MD5context;
982 unsigned char MD5digest[16];
984 struct berval digest;
986 digest.bv_val = MD5digest;
987 digest.bv_len = sizeof(MD5digest);
989 lutil_MD5Init( &MD5context );
990 lutil_MD5Update( &MD5context,
991 (const unsigned char *) passwd->bv_val, passwd->bv_len );
992 lutil_MD5Final( MD5digest, &MD5context );
994 return pw_string64( scheme, &digest, NULL );
999 static struct berval *hash_crypt(
1000 const struct pw_scheme *scheme,
1001 const struct berval *passwd )
1004 unsigned char salt[3];
1007 for( i=0; i<passwd->bv_len; i++) {
1008 if(passwd->bv_val[i] == '\0') {
1009 return NULL; /* NUL character in password */
1013 if( passwd->bv_val[i] != '\0' ) {
1014 return NULL; /* passwd must behave like a string */
1017 if( lutil_entropy( salt, sizeof(salt)) < 0 ) {
1021 salt[0] = crypt64[ salt[0] % (sizeof(crypt64)-1) ];
1022 salt[1] = crypt64[ salt[1] % (sizeof(crypt64)-1) ];
1025 hash.bv_val = crypt( passwd->bv_val, salt );
1027 if( hash.bv_val == NULL ) return NULL;
1029 hash.bv_len = strlen( hash.bv_val );
1031 if( hash.bv_len == 0 ) {
1035 return pw_string( scheme, &hash );