Home | History | Annotate | Line # | Download | only in liblutil
passwd.c revision 1.4.6.1
      1  1.4.6.1  pgoyette /*	$NetBSD: passwd.c,v 1.4.6.1 2017/03/20 06:56:15 pgoyette Exp $	*/
      2      1.2     lukem 
      3      1.4      tron /* $OpenLDAP$ */
      4      1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5      1.1     lukem  *
      6  1.4.6.1  pgoyette  * Copyright 1998-2016 The OpenLDAP Foundation.
      7      1.1     lukem  * All rights reserved.
      8      1.1     lukem  *
      9      1.1     lukem  * Redistribution and use in source and binary forms, with or without
     10      1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     11      1.1     lukem  * Public License.
     12      1.1     lukem  *
     13      1.1     lukem  * A copy of this license is available in the file LICENSE in the
     14      1.1     lukem  * top-level directory of the distribution or, alternatively, at
     15      1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     16      1.1     lukem  */
     17      1.1     lukem 
     18      1.1     lukem /*
     19      1.1     lukem  * int lutil_passwd(
     20      1.1     lukem  *	const struct berval *passwd,
     21      1.1     lukem  *	const struct berval *cred,
     22      1.1     lukem  *	const char **schemes )
     23      1.1     lukem  *
     24      1.1     lukem  * Returns true if user supplied credentials (cred) matches
     25      1.1     lukem  * the stored password (passwd).
     26      1.1     lukem  *
     27      1.1     lukem  * Due to the use of the crypt(3) function
     28      1.1     lukem  * this routine is NOT thread-safe.
     29      1.1     lukem  */
     30      1.1     lukem 
     31  1.4.6.1  pgoyette #include <sys/cdefs.h>
     32  1.4.6.1  pgoyette __RCSID("$NetBSD: passwd.c,v 1.4.6.1 2017/03/20 06:56:15 pgoyette Exp $");
     33  1.4.6.1  pgoyette 
     34      1.1     lukem #include "portable.h"
     35      1.1     lukem 
     36      1.1     lukem #include <stdio.h>
     37      1.1     lukem #include <ac/stdlib.h>
     38      1.1     lukem #include <ac/string.h>
     39      1.2     lukem #include <ac/time.h>
     40      1.1     lukem #include <ac/unistd.h>
     41      1.1     lukem 
     42      1.1     lukem #if defined(SLAPD_LMHASH)
     43      1.2     lukem #if defined(HAVE_OPENSSL)
     44      1.1     lukem #	include <openssl/des.h>
     45      1.2     lukem 
     46      1.2     lukem 
     47      1.2     lukem typedef des_cblock des_key;
     48      1.2     lukem typedef des_cblock des_data_block;
     49      1.2     lukem typedef des_key_schedule des_context;
     50      1.2     lukem #define des_failed(encrypted) 0
     51      1.2     lukem #define des_finish(key, schedule)
     52      1.2     lukem 
     53      1.2     lukem #elif defined(HAVE_MOZNSS)
     54      1.2     lukem /*
     55      1.2     lukem   hack hack hack
     56      1.2     lukem   We need to define this here so that nspr/obsolete/protypes.h will not be included
     57      1.2     lukem   if that file is included, it will create a uint32 typedef that will cause the
     58      1.2     lukem   one in lutil_sha1.h to blow up
     59      1.2     lukem */
     60      1.2     lukem #define PROTYPES_H 1
     61      1.2     lukem #	include <nss/pk11pub.h>
     62      1.2     lukem typedef PK11SymKey *des_key;
     63      1.2     lukem typedef unsigned char des_data_block[8];
     64      1.2     lukem typedef PK11Context *des_context[1];
     65      1.2     lukem #define DES_ENCRYPT CKA_ENCRYPT
     66      1.2     lukem 
     67      1.2     lukem #endif
     68      1.2     lukem 
     69      1.1     lukem #endif /* SLAPD_LMHASH */
     70      1.1     lukem 
     71      1.1     lukem #include <ac/param.h>
     72      1.1     lukem 
     73      1.1     lukem #ifdef SLAPD_CRYPT
     74      1.1     lukem # include <ac/crypt.h>
     75      1.1     lukem 
     76      1.1     lukem # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
     77      1.1     lukem #  ifdef HAVE_SHADOW_H
     78      1.1     lukem #	include <shadow.h>
     79      1.1     lukem #  endif
     80      1.1     lukem #  ifdef HAVE_PWD_H
     81      1.1     lukem #	include <pwd.h>
     82      1.1     lukem #  endif
     83      1.1     lukem #  ifdef HAVE_AIX_SECURITY
     84      1.1     lukem #	include <userpw.h>
     85      1.1     lukem #  endif
     86      1.1     lukem # endif
     87      1.1     lukem #endif
     88      1.1     lukem 
     89      1.1     lukem #include <lber.h>
     90      1.1     lukem 
     91      1.1     lukem #include "ldap_pvt.h"
     92      1.1     lukem #include "lber_pvt.h"
     93      1.1     lukem 
     94      1.1     lukem #include "lutil_md5.h"
     95      1.1     lukem #include "lutil_sha1.h"
     96      1.1     lukem #include "lutil.h"
     97      1.1     lukem 
     98      1.1     lukem static const unsigned char crypt64[] =
     99      1.1     lukem 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
    100      1.1     lukem 
    101      1.1     lukem #ifdef SLAPD_CRYPT
    102      1.1     lukem static char *salt_format = NULL;
    103      1.1     lukem static lutil_cryptfunc lutil_crypt;
    104      1.1     lukem lutil_cryptfunc *lutil_cryptptr = lutil_crypt;
    105      1.1     lukem #endif
    106      1.1     lukem 
    107      1.1     lukem /* KLUDGE:
    108      1.1     lukem  *  chk_fn is NULL iff name is {CLEARTEXT}
    109      1.1     lukem  *	otherwise, things will break
    110      1.1     lukem  */
    111      1.1     lukem struct pw_scheme {
    112      1.1     lukem 	struct berval name;
    113      1.1     lukem 	LUTIL_PASSWD_CHK_FUNC *chk_fn;
    114      1.1     lukem 	LUTIL_PASSWD_HASH_FUNC *hash_fn;
    115      1.1     lukem };
    116      1.1     lukem 
    117      1.1     lukem struct pw_slist {
    118      1.1     lukem 	struct pw_slist *next;
    119      1.1     lukem 	struct pw_scheme s;
    120      1.1     lukem };
    121      1.1     lukem 
    122      1.1     lukem /* password check routines */
    123      1.1     lukem 
    124      1.1     lukem #define	SALT_SIZE	4
    125      1.1     lukem 
    126      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_md5;
    127      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_smd5;
    128      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_smd5;
    129      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_md5;
    130      1.1     lukem 
    131      1.1     lukem 
    132      1.1     lukem #ifdef LUTIL_SHA1_BYTES
    133      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_ssha1;
    134      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_sha1;
    135      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_sha1;
    136      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_ssha1;
    137      1.1     lukem #endif
    138      1.1     lukem 
    139      1.1     lukem #ifdef SLAPD_LMHASH
    140      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_lanman;
    141      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_lanman;
    142      1.1     lukem #endif
    143      1.1     lukem 
    144      1.1     lukem #ifdef SLAPD_CRYPT
    145      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_crypt;
    146      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_crypt;
    147      1.1     lukem 
    148      1.1     lukem #if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
    149      1.1     lukem static LUTIL_PASSWD_CHK_FUNC chk_unix;
    150      1.1     lukem #endif
    151      1.1     lukem #endif
    152      1.1     lukem 
    153      1.1     lukem /* password hash routines */
    154      1.1     lukem 
    155      1.1     lukem #ifdef SLAPD_CLEARTEXT
    156      1.1     lukem static LUTIL_PASSWD_HASH_FUNC hash_clear;
    157      1.1     lukem #endif
    158      1.1     lukem 
    159      1.1     lukem static struct pw_slist *pw_schemes;
    160      1.1     lukem static int pw_inited;
    161      1.1     lukem 
    162      1.1     lukem static const struct pw_scheme pw_schemes_default[] =
    163      1.1     lukem {
    164      1.1     lukem #ifdef LUTIL_SHA1_BYTES
    165      1.1     lukem 	{ BER_BVC("{SSHA}"),		chk_ssha1, hash_ssha1 },
    166      1.1     lukem 	{ BER_BVC("{SHA}"),			chk_sha1, hash_sha1 },
    167      1.1     lukem #endif
    168      1.1     lukem 
    169      1.1     lukem 	{ BER_BVC("{SMD5}"),		chk_smd5, hash_smd5 },
    170      1.1     lukem 	{ BER_BVC("{MD5}"),			chk_md5, hash_md5 },
    171      1.1     lukem 
    172      1.1     lukem #ifdef SLAPD_LMHASH
    173      1.1     lukem 	{ BER_BVC("{LANMAN}"),		chk_lanman, hash_lanman },
    174      1.1     lukem #endif /* SLAPD_LMHASH */
    175      1.1     lukem 
    176      1.1     lukem #ifdef SLAPD_CRYPT
    177      1.1     lukem 	{ BER_BVC("{CRYPT}"),		chk_crypt, hash_crypt },
    178      1.1     lukem # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
    179      1.1     lukem 	{ BER_BVC("{UNIX}"),		chk_unix, NULL },
    180      1.1     lukem # endif
    181      1.1     lukem #endif
    182      1.1     lukem 
    183      1.1     lukem #ifdef SLAPD_CLEARTEXT
    184      1.1     lukem 	/* pseudo scheme */
    185      1.1     lukem 	{ BER_BVC("{CLEARTEXT}"),	NULL, hash_clear },
    186      1.1     lukem #endif
    187      1.1     lukem 
    188      1.1     lukem 	{ BER_BVNULL, NULL, NULL }
    189      1.1     lukem };
    190      1.1     lukem 
    191      1.1     lukem int lutil_passwd_add(
    192      1.1     lukem 	struct berval *scheme,
    193      1.1     lukem 	LUTIL_PASSWD_CHK_FUNC *chk,
    194      1.1     lukem 	LUTIL_PASSWD_HASH_FUNC *hash )
    195      1.1     lukem {
    196      1.1     lukem 	struct pw_slist *ptr;
    197      1.1     lukem 
    198      1.1     lukem 	if (!pw_inited) lutil_passwd_init();
    199      1.1     lukem 
    200      1.1     lukem 	ptr = ber_memalloc( sizeof( struct pw_slist ));
    201      1.1     lukem 	if (!ptr) return -1;
    202      1.1     lukem 	ptr->next = pw_schemes;
    203      1.1     lukem 	ptr->s.name = *scheme;
    204      1.1     lukem 	ptr->s.chk_fn = chk;
    205      1.1     lukem 	ptr->s.hash_fn = hash;
    206      1.1     lukem 	pw_schemes = ptr;
    207      1.1     lukem 	return 0;
    208      1.1     lukem }
    209      1.1     lukem 
    210      1.1     lukem void lutil_passwd_init()
    211      1.1     lukem {
    212      1.1     lukem 	struct pw_scheme *s;
    213      1.1     lukem 
    214      1.1     lukem 	pw_inited = 1;
    215      1.1     lukem 
    216      1.1     lukem 	for( s=(struct pw_scheme *)pw_schemes_default; s->name.bv_val; s++) {
    217      1.1     lukem 		if ( lutil_passwd_add( &s->name, s->chk_fn, s->hash_fn ) ) break;
    218      1.1     lukem 	}
    219      1.1     lukem }
    220      1.1     lukem 
    221      1.1     lukem void lutil_passwd_destroy()
    222      1.1     lukem {
    223      1.1     lukem 	struct pw_slist *ptr, *next;
    224      1.1     lukem 
    225      1.1     lukem 	for( ptr=pw_schemes; ptr; ptr=next ) {
    226      1.1     lukem 		next = ptr->next;
    227      1.1     lukem 		ber_memfree( ptr );
    228      1.1     lukem 	}
    229      1.1     lukem }
    230      1.1     lukem 
    231      1.1     lukem static const struct pw_scheme *get_scheme(
    232      1.1     lukem 	const char* scheme )
    233      1.1     lukem {
    234      1.1     lukem 	struct pw_slist *pws;
    235      1.1     lukem 	struct berval bv;
    236      1.1     lukem 
    237      1.1     lukem 	if (!pw_inited) lutil_passwd_init();
    238      1.1     lukem 
    239      1.1     lukem 	bv.bv_val = strchr( scheme, '}' );
    240      1.1     lukem 	if ( !bv.bv_val )
    241      1.1     lukem 		return NULL;
    242      1.1     lukem 
    243      1.1     lukem 	bv.bv_len = bv.bv_val - scheme + 1;
    244      1.1     lukem 	bv.bv_val = (char *) scheme;
    245      1.1     lukem 
    246      1.1     lukem 	for( pws=pw_schemes; pws; pws=pws->next ) {
    247      1.1     lukem 		if ( ber_bvstrcasecmp(&bv, &pws->s.name ) == 0 ) {
    248      1.1     lukem 			return &(pws->s);
    249      1.1     lukem 		}
    250      1.1     lukem 	}
    251      1.1     lukem 
    252      1.1     lukem 	return NULL;
    253      1.1     lukem }
    254      1.1     lukem 
    255      1.1     lukem int lutil_passwd_scheme(
    256      1.1     lukem 	const char* scheme )
    257      1.1     lukem {
    258      1.1     lukem 	if( scheme == NULL ) {
    259      1.1     lukem 		return 0;
    260      1.1     lukem 	}
    261      1.1     lukem 
    262      1.1     lukem 	return get_scheme(scheme) != NULL;
    263      1.1     lukem }
    264      1.1     lukem 
    265      1.1     lukem 
    266      1.1     lukem static int is_allowed_scheme(
    267      1.1     lukem 	const char* scheme,
    268      1.1     lukem 	const char** schemes )
    269      1.1     lukem {
    270      1.1     lukem 	int i;
    271      1.1     lukem 
    272      1.1     lukem 	if( schemes == NULL ) return 1;
    273      1.1     lukem 
    274      1.1     lukem 	for( i=0; schemes[i] != NULL; i++ ) {
    275      1.1     lukem 		if( strcasecmp( scheme, schemes[i] ) == 0 ) {
    276      1.1     lukem 			return 1;
    277      1.1     lukem 		}
    278      1.1     lukem 	}
    279      1.1     lukem 	return 0;
    280      1.1     lukem }
    281      1.1     lukem 
    282      1.1     lukem static struct berval *passwd_scheme(
    283      1.1     lukem 	const struct pw_scheme *scheme,
    284      1.1     lukem 	const struct berval * passwd,
    285      1.1     lukem 	struct berval *bv,
    286      1.1     lukem 	const char** allowed )
    287      1.1     lukem {
    288      1.1     lukem 	if( !is_allowed_scheme( scheme->name.bv_val, allowed ) ) {
    289      1.1     lukem 		return NULL;
    290      1.1     lukem 	}
    291      1.1     lukem 
    292      1.1     lukem 	if( passwd->bv_len >= scheme->name.bv_len ) {
    293      1.1     lukem 		if( strncasecmp( passwd->bv_val, scheme->name.bv_val, scheme->name.bv_len ) == 0 ) {
    294      1.1     lukem 			bv->bv_val = &passwd->bv_val[scheme->name.bv_len];
    295      1.1     lukem 			bv->bv_len = passwd->bv_len - scheme->name.bv_len;
    296      1.1     lukem 
    297      1.1     lukem 			return bv;
    298      1.1     lukem 		}
    299      1.1     lukem 	}
    300      1.1     lukem 
    301      1.1     lukem 	return NULL;
    302      1.1     lukem }
    303      1.1     lukem 
    304      1.1     lukem /*
    305      1.1     lukem  * Return 0 if creds are good.
    306      1.1     lukem  */
    307      1.1     lukem int
    308      1.1     lukem lutil_passwd(
    309      1.1     lukem 	const struct berval *passwd,	/* stored passwd */
    310      1.1     lukem 	const struct berval *cred,		/* user cred */
    311      1.1     lukem 	const char **schemes,
    312      1.1     lukem 	const char **text )
    313      1.1     lukem {
    314      1.1     lukem 	struct pw_slist *pws;
    315      1.1     lukem 
    316      1.1     lukem 	if ( text ) *text = NULL;
    317      1.1     lukem 
    318      1.1     lukem 	if (cred == NULL || cred->bv_len == 0 ||
    319      1.1     lukem 		passwd == NULL || passwd->bv_len == 0 )
    320      1.1     lukem 	{
    321      1.1     lukem 		return -1;
    322      1.1     lukem 	}
    323      1.1     lukem 
    324      1.1     lukem 	if (!pw_inited) lutil_passwd_init();
    325      1.1     lukem 
    326      1.1     lukem 	for( pws=pw_schemes; pws; pws=pws->next ) {
    327      1.1     lukem 		if( pws->s.chk_fn ) {
    328      1.1     lukem 			struct berval x;
    329      1.1     lukem 			struct berval *p = passwd_scheme( &(pws->s),
    330      1.1     lukem 				passwd, &x, schemes );
    331      1.1     lukem 
    332      1.1     lukem 			if( p != NULL ) {
    333      1.1     lukem 				return (pws->s.chk_fn)( &(pws->s.name), p, cred, text );
    334      1.1     lukem 			}
    335      1.1     lukem 		}
    336      1.1     lukem 	}
    337      1.1     lukem 
    338      1.1     lukem #ifdef SLAPD_CLEARTEXT
    339      1.1     lukem 	/* Do we think there is a scheme specifier here that we
    340      1.1     lukem 	 * didn't recognize? Assume a scheme name is at least 1 character.
    341      1.1     lukem 	 */
    342      1.1     lukem 	if (( passwd->bv_val[0] == '{' ) &&
    343      1.1     lukem 		( ber_bvchr( passwd, '}' ) > passwd->bv_val+1 ))
    344      1.1     lukem 	{
    345      1.1     lukem 		return 1;
    346      1.1     lukem 	}
    347      1.1     lukem 	if( is_allowed_scheme("{CLEARTEXT}", schemes ) ) {
    348      1.1     lukem 		return ( passwd->bv_len == cred->bv_len ) ?
    349      1.1     lukem 			memcmp( passwd->bv_val, cred->bv_val, passwd->bv_len )
    350      1.1     lukem 			: 1;
    351      1.1     lukem 	}
    352      1.1     lukem #endif
    353      1.1     lukem 	return 1;
    354      1.1     lukem }
    355      1.1     lukem 
    356      1.1     lukem int lutil_passwd_generate( struct berval *pw, ber_len_t len )
    357      1.1     lukem {
    358      1.1     lukem 
    359      1.1     lukem 	if( len < 1 ) return -1;
    360      1.1     lukem 
    361      1.1     lukem 	pw->bv_len = len;
    362      1.1     lukem 	pw->bv_val = ber_memalloc( len + 1 );
    363      1.1     lukem 
    364      1.1     lukem 	if( pw->bv_val == NULL ) {
    365      1.1     lukem 		return -1;
    366      1.1     lukem 	}
    367      1.1     lukem 
    368      1.1     lukem 	if( lutil_entropy( (unsigned char *) pw->bv_val, pw->bv_len) < 0 ) {
    369      1.1     lukem 		return -1;
    370      1.1     lukem 	}
    371      1.1     lukem 
    372      1.1     lukem 	for( len = 0; len < pw->bv_len; len++ ) {
    373      1.1     lukem 		pw->bv_val[len] = crypt64[
    374      1.1     lukem 			pw->bv_val[len] % (sizeof(crypt64)-1) ];
    375      1.1     lukem 	}
    376      1.1     lukem 
    377      1.1     lukem 	pw->bv_val[len] = '\0';
    378      1.1     lukem 
    379      1.1     lukem 	return 0;
    380      1.1     lukem }
    381      1.1     lukem 
    382      1.1     lukem int lutil_passwd_hash(
    383      1.1     lukem 	const struct berval * passwd,
    384      1.1     lukem 	const char * method,
    385      1.1     lukem 	struct berval *hash,
    386      1.1     lukem 	const char **text )
    387      1.1     lukem {
    388      1.1     lukem 	const struct pw_scheme *sc = get_scheme( method );
    389      1.1     lukem 
    390      1.1     lukem 	hash->bv_val = NULL;
    391      1.1     lukem 	hash->bv_len = 0;
    392      1.1     lukem 
    393      1.1     lukem 	if( sc == NULL ) {
    394      1.1     lukem 		if( text ) *text = "scheme not recognized";
    395      1.1     lukem 		return -1;
    396      1.1     lukem 	}
    397      1.1     lukem 
    398      1.1     lukem 	if( ! sc->hash_fn ) {
    399      1.1     lukem 		if( text ) *text = "scheme provided no hash function";
    400      1.1     lukem 		return -1;
    401      1.1     lukem 	}
    402      1.1     lukem 
    403      1.1     lukem 	if( text ) *text = NULL;
    404      1.1     lukem 
    405      1.1     lukem 	return (sc->hash_fn)( &sc->name, passwd, hash, text );
    406      1.1     lukem }
    407      1.1     lukem 
    408      1.1     lukem /* pw_string is only called when SLAPD_LMHASH or SLAPD_CRYPT is defined */
    409      1.1     lukem #if defined(SLAPD_LMHASH) || defined(SLAPD_CRYPT)
    410      1.1     lukem static int pw_string(
    411      1.1     lukem 	const struct berval *sc,
    412      1.1     lukem 	struct berval *passwd )
    413      1.1     lukem {
    414      1.1     lukem 	struct berval pw;
    415      1.1     lukem 
    416      1.1     lukem 	pw.bv_len = sc->bv_len + passwd->bv_len;
    417      1.1     lukem 	pw.bv_val = ber_memalloc( pw.bv_len + 1 );
    418      1.1     lukem 
    419      1.1     lukem 	if( pw.bv_val == NULL ) {
    420      1.1     lukem 		return LUTIL_PASSWD_ERR;
    421      1.1     lukem 	}
    422      1.1     lukem 
    423      1.1     lukem 	AC_MEMCPY( pw.bv_val, sc->bv_val, sc->bv_len );
    424      1.1     lukem 	AC_MEMCPY( &pw.bv_val[sc->bv_len], passwd->bv_val, passwd->bv_len );
    425      1.1     lukem 
    426      1.1     lukem 	pw.bv_val[pw.bv_len] = '\0';
    427      1.1     lukem 	*passwd = pw;
    428      1.1     lukem 
    429      1.1     lukem 	return LUTIL_PASSWD_OK;
    430      1.1     lukem }
    431      1.1     lukem #endif /* SLAPD_LMHASH || SLAPD_CRYPT */
    432      1.1     lukem 
    433      1.4      tron int lutil_passwd_string64(
    434      1.1     lukem 	const struct berval *sc,
    435      1.1     lukem 	const struct berval *hash,
    436      1.1     lukem 	struct berval *b64,
    437      1.1     lukem 	const struct berval *salt )
    438      1.1     lukem {
    439      1.1     lukem 	int rc;
    440      1.1     lukem 	struct berval string;
    441      1.1     lukem 	size_t b64len;
    442      1.1     lukem 
    443      1.1     lukem 	if( salt ) {
    444      1.1     lukem 		/* need to base64 combined string */
    445      1.1     lukem 		string.bv_len = hash->bv_len + salt->bv_len;
    446      1.1     lukem 		string.bv_val = ber_memalloc( string.bv_len + 1 );
    447      1.1     lukem 
    448      1.1     lukem 		if( string.bv_val == NULL ) {
    449      1.1     lukem 			return LUTIL_PASSWD_ERR;
    450      1.1     lukem 		}
    451      1.1     lukem 
    452      1.1     lukem 		AC_MEMCPY( string.bv_val, hash->bv_val,
    453      1.1     lukem 			hash->bv_len );
    454      1.1     lukem 		AC_MEMCPY( &string.bv_val[hash->bv_len], salt->bv_val,
    455      1.1     lukem 			salt->bv_len );
    456      1.1     lukem 		string.bv_val[string.bv_len] = '\0';
    457      1.1     lukem 
    458      1.1     lukem 	} else {
    459      1.1     lukem 		string = *hash;
    460      1.1     lukem 	}
    461      1.1     lukem 
    462      1.1     lukem 	b64len = LUTIL_BASE64_ENCODE_LEN( string.bv_len ) + 1;
    463      1.1     lukem 	b64->bv_len = b64len + sc->bv_len;
    464      1.1     lukem 	b64->bv_val = ber_memalloc( b64->bv_len + 1 );
    465      1.1     lukem 
    466      1.1     lukem 	if( b64->bv_val == NULL ) {
    467      1.1     lukem 		if( salt ) ber_memfree( string.bv_val );
    468      1.1     lukem 		return LUTIL_PASSWD_ERR;
    469      1.1     lukem 	}
    470      1.1     lukem 
    471      1.1     lukem 	AC_MEMCPY(b64->bv_val, sc->bv_val, sc->bv_len);
    472      1.1     lukem 
    473      1.1     lukem 	rc = lutil_b64_ntop(
    474      1.1     lukem 		(unsigned char *) string.bv_val, string.bv_len,
    475      1.1     lukem 		&b64->bv_val[sc->bv_len], b64len );
    476      1.1     lukem 
    477      1.1     lukem 	if( salt ) ber_memfree( string.bv_val );
    478      1.1     lukem 
    479      1.1     lukem 	if( rc < 0 ) {
    480      1.1     lukem 		return LUTIL_PASSWD_ERR;
    481      1.1     lukem 	}
    482      1.1     lukem 
    483      1.1     lukem 	/* recompute length */
    484      1.1     lukem 	b64->bv_len = sc->bv_len + rc;
    485      1.1     lukem 	assert( strlen(b64->bv_val) == b64->bv_len );
    486      1.1     lukem 	return LUTIL_PASSWD_OK;
    487      1.1     lukem }
    488      1.1     lukem 
    489      1.1     lukem /* PASSWORD CHECK ROUTINES */
    490      1.1     lukem 
    491      1.1     lukem #ifdef LUTIL_SHA1_BYTES
    492      1.1     lukem static int chk_ssha1(
    493      1.1     lukem 	const struct berval *sc,
    494      1.1     lukem 	const struct berval * passwd,
    495      1.1     lukem 	const struct berval * cred,
    496      1.1     lukem 	const char **text )
    497      1.1     lukem {
    498      1.1     lukem 	lutil_SHA1_CTX SHA1context;
    499      1.1     lukem 	unsigned char SHA1digest[LUTIL_SHA1_BYTES];
    500      1.1     lukem 	int rc;
    501      1.1     lukem 	unsigned char *orig_pass = NULL;
    502  1.4.6.1  pgoyette 	size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
    503      1.1     lukem 
    504      1.1     lukem 	/* safety check -- must have some salt */
    505  1.4.6.1  pgoyette 	if (decode_len <= sizeof(SHA1digest)) {
    506      1.1     lukem 		return LUTIL_PASSWD_ERR;
    507      1.1     lukem 	}
    508      1.1     lukem 
    509      1.1     lukem 	/* decode base64 password */
    510  1.4.6.1  pgoyette 	orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
    511      1.1     lukem 
    512      1.1     lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    513      1.1     lukem 
    514  1.4.6.1  pgoyette 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
    515      1.1     lukem 
    516      1.1     lukem 	/* safety check -- must have some salt */
    517      1.1     lukem 	if (rc <= (int)(sizeof(SHA1digest))) {
    518      1.1     lukem 		ber_memfree(orig_pass);
    519      1.1     lukem 		return LUTIL_PASSWD_ERR;
    520      1.1     lukem 	}
    521      1.1     lukem 
    522      1.1     lukem 	/* hash credentials with salt */
    523      1.1     lukem 	lutil_SHA1Init(&SHA1context);
    524      1.1     lukem 	lutil_SHA1Update(&SHA1context,
    525      1.1     lukem 		(const unsigned char *) cred->bv_val, cred->bv_len);
    526      1.1     lukem 	lutil_SHA1Update(&SHA1context,
    527      1.1     lukem 		(const unsigned char *) &orig_pass[sizeof(SHA1digest)],
    528      1.1     lukem 		rc - sizeof(SHA1digest));
    529      1.1     lukem 	lutil_SHA1Final(SHA1digest, &SHA1context);
    530      1.1     lukem 
    531      1.1     lukem 	/* compare */
    532      1.1     lukem 	rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
    533      1.1     lukem 	ber_memfree(orig_pass);
    534      1.1     lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    535      1.1     lukem }
    536      1.1     lukem 
    537      1.1     lukem static int chk_sha1(
    538      1.1     lukem 	const struct berval *sc,
    539      1.1     lukem 	const struct berval * passwd,
    540      1.1     lukem 	const struct berval * cred,
    541      1.1     lukem 	const char **text )
    542      1.1     lukem {
    543      1.1     lukem 	lutil_SHA1_CTX SHA1context;
    544      1.1     lukem 	unsigned char SHA1digest[LUTIL_SHA1_BYTES];
    545      1.1     lukem 	int rc;
    546      1.1     lukem 	unsigned char *orig_pass = NULL;
    547  1.4.6.1  pgoyette 	size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
    548      1.1     lukem 
    549      1.1     lukem 	/* safety check */
    550  1.4.6.1  pgoyette 	if (decode_len < sizeof(SHA1digest)) {
    551      1.1     lukem 		return LUTIL_PASSWD_ERR;
    552      1.1     lukem 	}
    553      1.1     lukem 
    554      1.1     lukem 	/* base64 un-encode password */
    555  1.4.6.1  pgoyette 	orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
    556      1.1     lukem 
    557      1.1     lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    558      1.1     lukem 
    559  1.4.6.1  pgoyette 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
    560      1.1     lukem 
    561      1.1     lukem 	if( rc != sizeof(SHA1digest) ) {
    562      1.1     lukem 		ber_memfree(orig_pass);
    563      1.1     lukem 		return LUTIL_PASSWD_ERR;
    564      1.1     lukem 	}
    565      1.1     lukem 
    566      1.1     lukem 	/* hash credentials with salt */
    567      1.1     lukem 	lutil_SHA1Init(&SHA1context);
    568      1.1     lukem 	lutil_SHA1Update(&SHA1context,
    569      1.1     lukem 		(const unsigned char *) cred->bv_val, cred->bv_len);
    570      1.1     lukem 	lutil_SHA1Final(SHA1digest, &SHA1context);
    571      1.1     lukem 
    572      1.1     lukem 	/* compare */
    573      1.1     lukem 	rc = memcmp((char *)orig_pass, (char *)SHA1digest, sizeof(SHA1digest));
    574      1.1     lukem 	ber_memfree(orig_pass);
    575      1.1     lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    576      1.1     lukem }
    577      1.1     lukem #endif
    578      1.1     lukem 
    579      1.1     lukem static int chk_smd5(
    580      1.1     lukem 	const struct berval *sc,
    581      1.1     lukem 	const struct berval * passwd,
    582      1.1     lukem 	const struct berval * cred,
    583      1.1     lukem 	const char **text )
    584      1.1     lukem {
    585      1.1     lukem 	lutil_MD5_CTX MD5context;
    586      1.1     lukem 	unsigned char MD5digest[LUTIL_MD5_BYTES];
    587      1.1     lukem 	int rc;
    588      1.1     lukem 	unsigned char *orig_pass = NULL;
    589  1.4.6.1  pgoyette 	size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
    590      1.1     lukem 
    591      1.1     lukem 	/* safety check */
    592  1.4.6.1  pgoyette 	if (decode_len <= sizeof(MD5digest)) {
    593      1.1     lukem 		return LUTIL_PASSWD_ERR;
    594      1.1     lukem 	}
    595      1.1     lukem 
    596      1.1     lukem 	/* base64 un-encode password */
    597  1.4.6.1  pgoyette 	orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
    598      1.1     lukem 
    599      1.1     lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    600      1.1     lukem 
    601  1.4.6.1  pgoyette 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
    602      1.1     lukem 
    603      1.1     lukem 	if (rc <= (int)(sizeof(MD5digest))) {
    604      1.1     lukem 		ber_memfree(orig_pass);
    605      1.1     lukem 		return LUTIL_PASSWD_ERR;
    606      1.1     lukem 	}
    607      1.1     lukem 
    608      1.1     lukem 	/* hash credentials with salt */
    609      1.1     lukem 	lutil_MD5Init(&MD5context);
    610      1.1     lukem 	lutil_MD5Update(&MD5context,
    611      1.1     lukem 		(const unsigned char *) cred->bv_val,
    612      1.1     lukem 		cred->bv_len );
    613      1.1     lukem 	lutil_MD5Update(&MD5context,
    614      1.1     lukem 		&orig_pass[sizeof(MD5digest)],
    615      1.1     lukem 		rc - sizeof(MD5digest));
    616      1.1     lukem 	lutil_MD5Final(MD5digest, &MD5context);
    617      1.1     lukem 
    618      1.1     lukem 	/* compare */
    619      1.1     lukem 	rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
    620      1.1     lukem 	ber_memfree(orig_pass);
    621      1.1     lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    622      1.1     lukem }
    623      1.1     lukem 
    624      1.1     lukem static int chk_md5(
    625      1.1     lukem 	const struct berval *sc,
    626      1.1     lukem 	const struct berval * passwd,
    627      1.1     lukem 	const struct berval * cred,
    628      1.1     lukem 	const char **text )
    629      1.1     lukem {
    630      1.1     lukem 	lutil_MD5_CTX MD5context;
    631      1.1     lukem 	unsigned char MD5digest[LUTIL_MD5_BYTES];
    632      1.1     lukem 	int rc;
    633      1.1     lukem 	unsigned char *orig_pass = NULL;
    634  1.4.6.1  pgoyette 	size_t decode_len = LUTIL_BASE64_DECODE_LEN(passwd->bv_len);
    635      1.1     lukem 
    636      1.1     lukem 	/* safety check */
    637  1.4.6.1  pgoyette 	if (decode_len < sizeof(MD5digest)) {
    638      1.1     lukem 		return LUTIL_PASSWD_ERR;
    639      1.1     lukem 	}
    640      1.1     lukem 
    641      1.1     lukem 	/* base64 un-encode password */
    642  1.4.6.1  pgoyette 	orig_pass = (unsigned char *) ber_memalloc(decode_len + 1);
    643      1.1     lukem 
    644      1.1     lukem 	if( orig_pass == NULL ) return LUTIL_PASSWD_ERR;
    645      1.1     lukem 
    646  1.4.6.1  pgoyette 	rc = lutil_b64_pton(passwd->bv_val, orig_pass, decode_len);
    647      1.1     lukem 	if ( rc != sizeof(MD5digest) ) {
    648      1.1     lukem 		ber_memfree(orig_pass);
    649      1.1     lukem 		return LUTIL_PASSWD_ERR;
    650      1.1     lukem 	}
    651      1.1     lukem 
    652      1.1     lukem 	/* hash credentials with salt */
    653      1.1     lukem 	lutil_MD5Init(&MD5context);
    654      1.1     lukem 	lutil_MD5Update(&MD5context,
    655      1.1     lukem 		(const unsigned char *) cred->bv_val,
    656      1.1     lukem 		cred->bv_len );
    657      1.1     lukem 	lutil_MD5Final(MD5digest, &MD5context);
    658      1.1     lukem 
    659      1.1     lukem 	/* compare */
    660      1.1     lukem 	rc = memcmp((char *)orig_pass, (char *)MD5digest, sizeof(MD5digest));
    661      1.1     lukem 	ber_memfree(orig_pass);
    662      1.1     lukem 	return rc ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    663      1.1     lukem }
    664      1.1     lukem 
    665      1.1     lukem #ifdef SLAPD_LMHASH
    666      1.2     lukem 
    667      1.2     lukem #if defined(HAVE_OPENSSL)
    668      1.2     lukem 
    669      1.2     lukem /*
    670      1.2     lukem  * abstract away setting the parity.
    671      1.2     lukem  */
    672      1.2     lukem static void
    673      1.2     lukem des_set_key_and_parity( des_key *key, unsigned char *keyData)
    674      1.2     lukem {
    675      1.2     lukem     memcpy(key, keyData, 8);
    676      1.2     lukem     des_set_odd_parity( key );
    677      1.2     lukem }
    678      1.2     lukem 
    679      1.2     lukem 
    680      1.2     lukem #elif defined(HAVE_MOZNSS)
    681      1.2     lukem 
    682      1.2     lukem /*
    683      1.2     lukem  * implement MozNSS wrappers for the openSSL calls
    684      1.2     lukem  */
    685      1.2     lukem static void
    686      1.2     lukem des_set_key_and_parity( des_key *key, unsigned char *keyData)
    687      1.2     lukem {
    688      1.2     lukem     SECItem keyDataItem;
    689      1.2     lukem     PK11SlotInfo *slot;
    690      1.2     lukem     *key = NULL;
    691      1.2     lukem 
    692      1.2     lukem     keyDataItem.data = keyData;
    693      1.2     lukem     keyDataItem.len = 8;
    694      1.2     lukem 
    695      1.2     lukem     slot = PK11_GetBestSlot(CKM_DES_ECB, NULL);
    696      1.2     lukem     if (slot == NULL) {
    697      1.2     lukem 	return;
    698      1.2     lukem     }
    699      1.2     lukem 
    700      1.2     lukem     /* NOTE: this will not work in FIPS mode. In order to make lmhash
    701      1.2     lukem      * work in fips mode we need to define a LMHASH pbe mechanism and
    702      1.2     lukem      * do the fulll key derivation inside the token */
    703      1.2     lukem     *key = PK11_ImportSymKey(slot, CKM_DES_ECB, PK11_OriginGenerated,
    704      1.2     lukem 		CKA_ENCRYPT, &keyDataItem, NULL);
    705      1.2     lukem }
    706      1.2     lukem 
    707      1.2     lukem static void
    708      1.2     lukem des_set_key_unchecked( des_key *key, des_context ctxt )
    709      1.2     lukem {
    710      1.2     lukem     ctxt[0] = NULL;
    711      1.2     lukem 
    712      1.2     lukem     /* handle error conditions from previous call */
    713      1.2     lukem     if (!*key) {
    714      1.2     lukem 	return;
    715      1.2     lukem     }
    716      1.2     lukem 
    717      1.2     lukem     ctxt[0] = PK11_CreateContextBySymKey(CKM_DES_ECB, CKA_ENCRYPT, *key, NULL);
    718      1.2     lukem }
    719      1.2     lukem 
    720      1.2     lukem static void
    721      1.2     lukem des_ecb_encrypt( des_data_block *plain, des_data_block *encrypted,
    722      1.2     lukem 			des_context ctxt, int op)
    723      1.2     lukem {
    724      1.2     lukem     SECStatus rv;
    725      1.2     lukem     int size;
    726      1.2     lukem 
    727      1.2     lukem     if (ctxt[0] == NULL) {
    728      1.2     lukem 	/* need to fail here...  */
    729      1.2     lukem 	memset(encrypted, 0, sizeof(des_data_block));
    730      1.2     lukem 	return;
    731      1.2     lukem     }
    732      1.2     lukem     rv = PK11_CipherOp(ctxt[0], (unsigned char *)&encrypted[0],
    733      1.2     lukem 			&size, sizeof(des_data_block),
    734      1.2     lukem 			(unsigned char *)&plain[0], sizeof(des_data_block));
    735      1.2     lukem     if (rv != SECSuccess) {
    736      1.2     lukem 	/* signal failure */
    737      1.2     lukem 	memset(encrypted, 0, sizeof(des_data_block));
    738      1.2     lukem 	return;
    739      1.2     lukem     }
    740      1.2     lukem     return;
    741      1.2     lukem }
    742      1.2     lukem 
    743      1.2     lukem static int
    744      1.2     lukem des_failed(des_data_block *encrypted)
    745      1.2     lukem {
    746      1.2     lukem    static const des_data_block zero = { 0 };
    747      1.2     lukem    return memcmp(encrypted, zero, sizeof(zero)) == 0;
    748      1.2     lukem }
    749      1.2     lukem 
    750      1.2     lukem static void
    751      1.2     lukem des_finish(des_key *key, des_context ctxt)
    752      1.2     lukem {
    753      1.2     lukem      if (*key) {
    754      1.2     lukem 	PK11_FreeSymKey(*key);
    755      1.2     lukem 	*key = NULL;
    756      1.2     lukem      }
    757      1.2     lukem      if (ctxt[0]) {
    758      1.2     lukem 	PK11_Finalize(ctxt[0]);
    759      1.2     lukem 	PK11_DestroyContext(ctxt[0], PR_TRUE);
    760      1.2     lukem 	ctxt[0] = NULL;
    761      1.2     lukem      }
    762      1.2     lukem }
    763      1.2     lukem 
    764      1.2     lukem #endif
    765      1.2     lukem 
    766      1.1     lukem /* pseudocode from RFC2433
    767      1.1     lukem  * A.2 LmPasswordHash()
    768      1.1     lukem  *
    769      1.1     lukem  *    LmPasswordHash(
    770      1.1     lukem  *    IN  0-to-14-oem-char Password,
    771      1.1     lukem  *    OUT 16-octet         PasswordHash )
    772      1.1     lukem  *    {
    773      1.1     lukem  *       Set UcasePassword to the uppercased Password
    774      1.1     lukem  *       Zero pad UcasePassword to 14 characters
    775      1.1     lukem  *
    776      1.1     lukem  *       DesHash( 1st 7-octets of UcasePassword,
    777      1.1     lukem  *                giving 1st 8-octets of PasswordHash )
    778      1.1     lukem  *
    779      1.1     lukem  *       DesHash( 2nd 7-octets of UcasePassword,
    780      1.1     lukem  *                giving 2nd 8-octets of PasswordHash )
    781      1.1     lukem  *    }
    782      1.1     lukem  *
    783      1.1     lukem  *
    784      1.1     lukem  * A.3 DesHash()
    785      1.1     lukem  *
    786      1.1     lukem  *    DesHash(
    787      1.1     lukem  *    IN  7-octet Clear,
    788      1.1     lukem  *    OUT 8-octet Cypher )
    789      1.1     lukem  *    {
    790      1.1     lukem  *        *
    791      1.1     lukem  *        * Make Cypher an irreversibly encrypted form of Clear by
    792      1.1     lukem  *        * encrypting known text using Clear as the secret key.
    793      1.1     lukem  *        * The known text consists of the string
    794      1.1     lukem  *        *
    795      1.1     lukem  *        *              KGS!@#$%
    796      1.1     lukem  *        *
    797      1.1     lukem  *
    798      1.1     lukem  *       Set StdText to "KGS!@#$%"
    799      1.1     lukem  *       DesEncrypt( StdText, Clear, giving Cypher )
    800      1.1     lukem  *    }
    801      1.1     lukem  *
    802      1.1     lukem  *
    803      1.1     lukem  * A.4 DesEncrypt()
    804      1.1     lukem  *
    805      1.1     lukem  *    DesEncrypt(
    806      1.1     lukem  *    IN  8-octet Clear,
    807      1.1     lukem  *    IN  7-octet Key,
    808      1.1     lukem  *    OUT 8-octet Cypher )
    809      1.1     lukem  *    {
    810      1.1     lukem  *        *
    811      1.1     lukem  *        * Use the DES encryption algorithm [4] in ECB mode [9]
    812      1.1     lukem  *        * to encrypt Clear into Cypher such that Cypher can
    813      1.1     lukem  *        * only be decrypted back to Clear by providing Key.
    814      1.1     lukem  *        * Note that the DES algorithm takes as input a 64-bit
    815      1.1     lukem  *        * stream where the 8th, 16th, 24th, etc.  bits are
    816      1.1     lukem  *        * parity bits ignored by the encrypting algorithm.
    817      1.1     lukem  *        * Unless you write your own DES to accept 56-bit input
    818      1.1     lukem  *        * without parity, you will need to insert the parity bits
    819      1.1     lukem  *        * yourself.
    820      1.1     lukem  *        *
    821      1.1     lukem  *    }
    822      1.1     lukem  */
    823      1.1     lukem 
    824      1.1     lukem static void lmPasswd_to_key(
    825      1.1     lukem 	const char *lmPasswd,
    826      1.2     lukem 	des_key *key)
    827      1.1     lukem {
    828      1.1     lukem 	const unsigned char *lpw = (const unsigned char *) lmPasswd;
    829      1.2     lukem 	unsigned char k[8];
    830      1.1     lukem 
    831      1.1     lukem 	/* make room for parity bits */
    832      1.1     lukem 	k[0] = lpw[0];
    833      1.1     lukem 	k[1] = ((lpw[0] & 0x01) << 7) | (lpw[1] >> 1);
    834      1.1     lukem 	k[2] = ((lpw[1] & 0x03) << 6) | (lpw[2] >> 2);
    835      1.1     lukem 	k[3] = ((lpw[2] & 0x07) << 5) | (lpw[3] >> 3);
    836      1.1     lukem 	k[4] = ((lpw[3] & 0x0F) << 4) | (lpw[4] >> 4);
    837      1.1     lukem 	k[5] = ((lpw[4] & 0x1F) << 3) | (lpw[5] >> 5);
    838      1.1     lukem 	k[6] = ((lpw[5] & 0x3F) << 2) | (lpw[6] >> 6);
    839      1.1     lukem 	k[7] = ((lpw[6] & 0x7F) << 1);
    840      1.1     lukem 
    841      1.2     lukem 	des_set_key_and_parity( key, k );
    842      1.1     lukem }
    843      1.1     lukem 
    844      1.1     lukem static int chk_lanman(
    845      1.1     lukem 	const struct berval *scheme,
    846      1.1     lukem 	const struct berval *passwd,
    847      1.1     lukem 	const struct berval *cred,
    848      1.1     lukem 	const char **text )
    849      1.1     lukem {
    850      1.2     lukem 	ber_len_t i;
    851      1.1     lukem 	char UcasePassword[15];
    852      1.2     lukem 	des_key key;
    853      1.2     lukem 	des_context schedule;
    854      1.2     lukem 	des_data_block StdText = "KGS!@#$%";
    855      1.2     lukem 	des_data_block PasswordHash1, PasswordHash2;
    856      1.1     lukem 	char PasswordHash[33], storedPasswordHash[33];
    857      1.1     lukem 
    858      1.1     lukem 	for( i=0; i<cred->bv_len; i++) {
    859      1.1     lukem 		if(cred->bv_val[i] == '\0') {
    860      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    861      1.1     lukem 		}
    862      1.1     lukem 	}
    863      1.1     lukem 
    864      1.1     lukem 	if( cred->bv_val[i] != '\0' ) {
    865      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
    866      1.1     lukem 	}
    867      1.1     lukem 
    868      1.1     lukem 	strncpy( UcasePassword, cred->bv_val, 14 );
    869      1.1     lukem 	UcasePassword[14] = '\0';
    870      1.1     lukem 	ldap_pvt_str2upper( UcasePassword );
    871      1.1     lukem 
    872      1.1     lukem 	lmPasswd_to_key( UcasePassword, &key );
    873      1.1     lukem 	des_set_key_unchecked( &key, schedule );
    874      1.1     lukem 	des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
    875      1.2     lukem 
    876      1.2     lukem 	if (des_failed(&PasswordHash1)) {
    877      1.2     lukem 	    return LUTIL_PASSWD_ERR;
    878      1.2     lukem 	}
    879      1.1     lukem 
    880      1.1     lukem 	lmPasswd_to_key( &UcasePassword[7], &key );
    881      1.1     lukem 	des_set_key_unchecked( &key, schedule );
    882      1.1     lukem 	des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
    883      1.2     lukem 	if (des_failed(&PasswordHash2)) {
    884      1.2     lukem 	    return LUTIL_PASSWD_ERR;
    885      1.2     lukem 	}
    886      1.2     lukem 
    887      1.2     lukem 	des_finish( &key, schedule );
    888      1.1     lukem 
    889      1.1     lukem 	sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
    890      1.1     lukem 		PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
    891      1.1     lukem 		PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
    892      1.1     lukem 		PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
    893      1.1     lukem 		PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
    894      1.1     lukem 
    895      1.1     lukem 	/* as a precaution convert stored password hash to lower case */
    896      1.1     lukem 	strncpy( storedPasswordHash, passwd->bv_val, 32 );
    897      1.1     lukem 	storedPasswordHash[32] = '\0';
    898      1.1     lukem 	ldap_pvt_str2lower( storedPasswordHash );
    899      1.1     lukem 
    900      1.1     lukem 	return memcmp( PasswordHash, storedPasswordHash, 32) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    901      1.1     lukem }
    902      1.1     lukem #endif /* SLAPD_LMHASH */
    903      1.1     lukem 
    904      1.1     lukem #ifdef SLAPD_CRYPT
    905      1.1     lukem static int lutil_crypt(
    906      1.1     lukem 	const char *key,
    907      1.1     lukem 	const char *salt,
    908      1.1     lukem 	char **hash )
    909      1.1     lukem {
    910      1.1     lukem 	char *cr = crypt( key, salt );
    911      1.1     lukem 	int rc;
    912      1.1     lukem 
    913      1.1     lukem 	if( cr == NULL || cr[0] == '\0' ) {
    914      1.1     lukem 		/* salt must have been invalid */
    915      1.1     lukem 		rc = LUTIL_PASSWD_ERR;
    916      1.1     lukem 	} else {
    917      1.1     lukem 		if ( hash ) {
    918      1.1     lukem 			*hash = ber_strdup( cr );
    919      1.1     lukem 			rc = LUTIL_PASSWD_OK;
    920      1.1     lukem 		} else {
    921      1.1     lukem 			rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
    922      1.1     lukem 		}
    923      1.1     lukem 	}
    924      1.1     lukem 	return rc;
    925      1.1     lukem }
    926      1.1     lukem 
    927      1.1     lukem static int chk_crypt(
    928      1.1     lukem 	const struct berval *sc,
    929      1.1     lukem 	const struct berval * passwd,
    930      1.1     lukem 	const struct berval * cred,
    931      1.1     lukem 	const char **text )
    932      1.1     lukem {
    933      1.1     lukem 	unsigned int i;
    934      1.1     lukem 
    935      1.1     lukem 	for( i=0; i<cred->bv_len; i++) {
    936      1.1     lukem 		if(cred->bv_val[i] == '\0') {
    937      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    938      1.1     lukem 		}
    939      1.1     lukem 	}
    940      1.1     lukem 
    941      1.1     lukem 	if( cred->bv_val[i] != '\0' ) {
    942      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* cred must behave like a string */
    943      1.1     lukem 	}
    944      1.1     lukem 
    945      1.1     lukem 	if( passwd->bv_len < 2 ) {
    946      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* passwd must be at least two characters long */
    947      1.1     lukem 	}
    948      1.1     lukem 
    949      1.1     lukem 	for( i=0; i<passwd->bv_len; i++) {
    950      1.1     lukem 		if(passwd->bv_val[i] == '\0') {
    951      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    952      1.1     lukem 		}
    953      1.1     lukem 	}
    954      1.1     lukem 
    955      1.1     lukem 	if( passwd->bv_val[i] != '\0' ) {
    956      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
    957      1.1     lukem 	}
    958      1.1     lukem 
    959      1.1     lukem 	return lutil_cryptptr( cred->bv_val, passwd->bv_val, NULL );
    960      1.1     lukem }
    961      1.1     lukem 
    962      1.1     lukem # if defined( HAVE_GETPWNAM ) && defined( HAVE_STRUCT_PASSWD_PW_PASSWD )
    963      1.1     lukem static int chk_unix(
    964      1.1     lukem 	const struct berval *sc,
    965      1.1     lukem 	const struct berval * passwd,
    966      1.1     lukem 	const struct berval * cred,
    967      1.1     lukem 	const char **text )
    968      1.1     lukem {
    969      1.1     lukem 	unsigned int i;
    970      1.1     lukem 	char *pw;
    971      1.1     lukem 
    972      1.1     lukem 	for( i=0; i<cred->bv_len; i++) {
    973      1.1     lukem 		if(cred->bv_val[i] == '\0') {
    974      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    975      1.1     lukem 		}
    976      1.1     lukem 	}
    977      1.1     lukem 	if( cred->bv_val[i] != '\0' ) {
    978      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* cred must behave like a string */
    979      1.1     lukem 	}
    980      1.1     lukem 
    981      1.1     lukem 	for( i=0; i<passwd->bv_len; i++) {
    982      1.1     lukem 		if(passwd->bv_val[i] == '\0') {
    983      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
    984      1.1     lukem 		}
    985      1.1     lukem 	}
    986      1.1     lukem 
    987      1.1     lukem 	if( passwd->bv_val[i] != '\0' ) {
    988      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
    989      1.1     lukem 	}
    990      1.1     lukem 
    991      1.1     lukem 	{
    992      1.1     lukem 		struct passwd *pwd = getpwnam(passwd->bv_val);
    993      1.1     lukem 
    994      1.1     lukem 		if(pwd == NULL) {
    995      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* not found */
    996      1.1     lukem 		}
    997      1.1     lukem 
    998      1.1     lukem 		pw = pwd->pw_passwd;
    999      1.1     lukem 	}
   1000      1.1     lukem #  ifdef HAVE_GETSPNAM
   1001      1.1     lukem 	{
   1002      1.1     lukem 		struct spwd *spwd = getspnam(passwd->bv_val);
   1003      1.1     lukem 
   1004      1.1     lukem 		if(spwd != NULL) {
   1005      1.1     lukem 			pw = spwd->sp_pwdp;
   1006      1.1     lukem 		}
   1007      1.1     lukem 	}
   1008      1.1     lukem #  endif
   1009      1.1     lukem #  ifdef HAVE_AIX_SECURITY
   1010      1.1     lukem 	{
   1011      1.1     lukem 		struct userpw *upw = getuserpw(passwd->bv_val);
   1012      1.1     lukem 
   1013      1.1     lukem 		if (upw != NULL) {
   1014      1.1     lukem 			pw = upw->upw_passwd;
   1015      1.1     lukem 		}
   1016      1.1     lukem 	}
   1017      1.1     lukem #  endif
   1018      1.1     lukem 
   1019      1.1     lukem 	if( pw == NULL || pw[0] == '\0' || pw[1] == '\0' ) {
   1020      1.1     lukem 		/* password must must be at least two characters long */
   1021      1.1     lukem 		return LUTIL_PASSWD_ERR;
   1022      1.1     lukem 	}
   1023      1.1     lukem 
   1024      1.1     lukem 	return lutil_cryptptr( cred->bv_val, pw, NULL );
   1025      1.1     lukem }
   1026      1.1     lukem # endif
   1027      1.1     lukem #endif
   1028      1.1     lukem 
   1029      1.1     lukem /* PASSWORD GENERATION ROUTINES */
   1030      1.1     lukem 
   1031      1.1     lukem #ifdef LUTIL_SHA1_BYTES
   1032      1.1     lukem static int hash_ssha1(
   1033      1.1     lukem 	const struct berval *scheme,
   1034      1.1     lukem 	const struct berval  *passwd,
   1035      1.1     lukem 	struct berval *hash,
   1036      1.1     lukem 	const char **text )
   1037      1.1     lukem {
   1038      1.1     lukem 	lutil_SHA1_CTX  SHA1context;
   1039      1.1     lukem 	unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
   1040      1.1     lukem 	char            saltdata[SALT_SIZE];
   1041      1.1     lukem 	struct berval digest;
   1042      1.1     lukem 	struct berval salt;
   1043      1.1     lukem 
   1044      1.1     lukem 	digest.bv_val = (char *) SHA1digest;
   1045      1.1     lukem 	digest.bv_len = sizeof(SHA1digest);
   1046      1.1     lukem 	salt.bv_val = saltdata;
   1047      1.1     lukem 	salt.bv_len = sizeof(saltdata);
   1048      1.1     lukem 
   1049      1.1     lukem 	if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
   1050      1.1     lukem 		return LUTIL_PASSWD_ERR;
   1051      1.1     lukem 	}
   1052      1.1     lukem 
   1053      1.1     lukem 	lutil_SHA1Init( &SHA1context );
   1054      1.1     lukem 	lutil_SHA1Update( &SHA1context,
   1055      1.1     lukem 		(const unsigned char *)passwd->bv_val, passwd->bv_len );
   1056      1.1     lukem 	lutil_SHA1Update( &SHA1context,
   1057      1.1     lukem 		(const unsigned char *)salt.bv_val, salt.bv_len );
   1058      1.1     lukem 	lutil_SHA1Final( SHA1digest, &SHA1context );
   1059      1.1     lukem 
   1060      1.4      tron 	return lutil_passwd_string64( scheme, &digest, hash, &salt);
   1061      1.1     lukem }
   1062      1.1     lukem 
   1063      1.1     lukem static int hash_sha1(
   1064      1.1     lukem 	const struct berval *scheme,
   1065      1.1     lukem 	const struct berval  *passwd,
   1066      1.1     lukem 	struct berval *hash,
   1067      1.1     lukem 	const char **text )
   1068      1.1     lukem {
   1069      1.1     lukem 	lutil_SHA1_CTX  SHA1context;
   1070      1.1     lukem 	unsigned char   SHA1digest[LUTIL_SHA1_BYTES];
   1071      1.1     lukem 	struct berval digest;
   1072      1.1     lukem 	digest.bv_val = (char *) SHA1digest;
   1073      1.1     lukem 	digest.bv_len = sizeof(SHA1digest);
   1074      1.1     lukem 
   1075      1.1     lukem 	lutil_SHA1Init( &SHA1context );
   1076      1.1     lukem 	lutil_SHA1Update( &SHA1context,
   1077      1.1     lukem 		(const unsigned char *)passwd->bv_val, passwd->bv_len );
   1078      1.1     lukem 	lutil_SHA1Final( SHA1digest, &SHA1context );
   1079      1.1     lukem 
   1080      1.4      tron 	return lutil_passwd_string64( scheme, &digest, hash, NULL);
   1081      1.1     lukem }
   1082      1.1     lukem #endif
   1083      1.1     lukem 
   1084      1.1     lukem static int hash_smd5(
   1085      1.1     lukem 	const struct berval *scheme,
   1086      1.1     lukem 	const struct berval  *passwd,
   1087      1.1     lukem 	struct berval *hash,
   1088      1.1     lukem 	const char **text )
   1089      1.1     lukem {
   1090      1.1     lukem 	lutil_MD5_CTX   MD5context;
   1091      1.1     lukem 	unsigned char   MD5digest[LUTIL_MD5_BYTES];
   1092      1.1     lukem 	char            saltdata[SALT_SIZE];
   1093      1.1     lukem 	struct berval digest;
   1094      1.1     lukem 	struct berval salt;
   1095      1.1     lukem 
   1096      1.1     lukem 	digest.bv_val = (char *) MD5digest;
   1097      1.1     lukem 	digest.bv_len = sizeof(MD5digest);
   1098      1.1     lukem 	salt.bv_val = saltdata;
   1099      1.1     lukem 	salt.bv_len = sizeof(saltdata);
   1100      1.1     lukem 
   1101      1.1     lukem 	if( lutil_entropy( (unsigned char *) salt.bv_val, salt.bv_len) < 0 ) {
   1102      1.1     lukem 		return LUTIL_PASSWD_ERR;
   1103      1.1     lukem 	}
   1104      1.1     lukem 
   1105      1.1     lukem 	lutil_MD5Init( &MD5context );
   1106      1.1     lukem 	lutil_MD5Update( &MD5context,
   1107      1.1     lukem 		(const unsigned char *) passwd->bv_val, passwd->bv_len );
   1108      1.1     lukem 	lutil_MD5Update( &MD5context,
   1109      1.1     lukem 		(const unsigned char *) salt.bv_val, salt.bv_len );
   1110      1.1     lukem 	lutil_MD5Final( MD5digest, &MD5context );
   1111      1.1     lukem 
   1112      1.4      tron 	return lutil_passwd_string64( scheme, &digest, hash, &salt );
   1113      1.1     lukem }
   1114      1.1     lukem 
   1115      1.1     lukem static int hash_md5(
   1116      1.1     lukem 	const struct berval *scheme,
   1117      1.1     lukem 	const struct berval  *passwd,
   1118      1.1     lukem 	struct berval *hash,
   1119      1.1     lukem 	const char **text )
   1120      1.1     lukem {
   1121      1.1     lukem 	lutil_MD5_CTX   MD5context;
   1122      1.1     lukem 	unsigned char   MD5digest[LUTIL_MD5_BYTES];
   1123      1.1     lukem 
   1124      1.1     lukem 	struct berval digest;
   1125      1.1     lukem 
   1126      1.1     lukem 	digest.bv_val = (char *) MD5digest;
   1127      1.1     lukem 	digest.bv_len = sizeof(MD5digest);
   1128      1.1     lukem 
   1129      1.1     lukem 	lutil_MD5Init( &MD5context );
   1130      1.1     lukem 	lutil_MD5Update( &MD5context,
   1131      1.1     lukem 		(const unsigned char *) passwd->bv_val, passwd->bv_len );
   1132      1.1     lukem 	lutil_MD5Final( MD5digest, &MD5context );
   1133      1.1     lukem 
   1134      1.4      tron 	return lutil_passwd_string64( scheme, &digest, hash, NULL );
   1135      1.1     lukem ;
   1136      1.1     lukem }
   1137      1.1     lukem 
   1138      1.1     lukem #ifdef SLAPD_LMHASH
   1139      1.1     lukem static int hash_lanman(
   1140      1.1     lukem 	const struct berval *scheme,
   1141      1.1     lukem 	const struct berval *passwd,
   1142      1.1     lukem 	struct berval *hash,
   1143      1.1     lukem 	const char **text )
   1144      1.1     lukem {
   1145      1.1     lukem 
   1146      1.2     lukem 	ber_len_t i;
   1147      1.1     lukem 	char UcasePassword[15];
   1148      1.2     lukem 	des_key key;
   1149      1.2     lukem 	des_context schedule;
   1150      1.2     lukem 	des_data_block StdText = "KGS!@#$%";
   1151      1.2     lukem 	des_data_block PasswordHash1, PasswordHash2;
   1152      1.1     lukem 	char PasswordHash[33];
   1153      1.1     lukem 
   1154      1.1     lukem 	for( i=0; i<passwd->bv_len; i++) {
   1155      1.1     lukem 		if(passwd->bv_val[i] == '\0') {
   1156      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
   1157      1.1     lukem 		}
   1158      1.1     lukem 	}
   1159      1.1     lukem 
   1160      1.1     lukem 	if( passwd->bv_val[i] != '\0' ) {
   1161      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
   1162      1.1     lukem 	}
   1163      1.1     lukem 
   1164      1.1     lukem 	strncpy( UcasePassword, passwd->bv_val, 14 );
   1165      1.1     lukem 	UcasePassword[14] = '\0';
   1166      1.1     lukem 	ldap_pvt_str2upper( UcasePassword );
   1167      1.1     lukem 
   1168      1.1     lukem 	lmPasswd_to_key( UcasePassword, &key );
   1169      1.1     lukem 	des_set_key_unchecked( &key, schedule );
   1170      1.1     lukem 	des_ecb_encrypt( &StdText, &PasswordHash1, schedule , DES_ENCRYPT );
   1171      1.1     lukem 
   1172      1.1     lukem 	lmPasswd_to_key( &UcasePassword[7], &key );
   1173      1.1     lukem 	des_set_key_unchecked( &key, schedule );
   1174      1.1     lukem 	des_ecb_encrypt( &StdText, &PasswordHash2, schedule , DES_ENCRYPT );
   1175      1.1     lukem 
   1176      1.1     lukem 	sprintf( PasswordHash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
   1177      1.1     lukem 		PasswordHash1[0],PasswordHash1[1],PasswordHash1[2],PasswordHash1[3],
   1178      1.1     lukem 		PasswordHash1[4],PasswordHash1[5],PasswordHash1[6],PasswordHash1[7],
   1179      1.1     lukem 		PasswordHash2[0],PasswordHash2[1],PasswordHash2[2],PasswordHash2[3],
   1180      1.1     lukem 		PasswordHash2[4],PasswordHash2[5],PasswordHash2[6],PasswordHash2[7] );
   1181      1.1     lukem 
   1182      1.1     lukem 	hash->bv_val = PasswordHash;
   1183      1.1     lukem 	hash->bv_len = 32;
   1184      1.1     lukem 
   1185      1.1     lukem 	return pw_string( scheme, hash );
   1186      1.1     lukem }
   1187      1.1     lukem #endif /* SLAPD_LMHASH */
   1188      1.1     lukem 
   1189      1.1     lukem #ifdef SLAPD_CRYPT
   1190      1.1     lukem static int hash_crypt(
   1191      1.1     lukem 	const struct berval *scheme,
   1192      1.1     lukem 	const struct berval *passwd,
   1193      1.1     lukem 	struct berval *hash,
   1194      1.1     lukem 	const char **text )
   1195      1.1     lukem {
   1196      1.1     lukem 	unsigned char salt[32];	/* salt suitable for most anything */
   1197      1.1     lukem 	unsigned int i;
   1198      1.1     lukem 	char *save;
   1199      1.1     lukem 	int rc;
   1200      1.1     lukem 
   1201      1.1     lukem 	for( i=0; i<passwd->bv_len; i++) {
   1202      1.1     lukem 		if(passwd->bv_val[i] == '\0') {
   1203      1.1     lukem 			return LUTIL_PASSWD_ERR;	/* NUL character in password */
   1204      1.1     lukem 		}
   1205      1.1     lukem 	}
   1206      1.1     lukem 
   1207      1.1     lukem 	if( passwd->bv_val[i] != '\0' ) {
   1208      1.1     lukem 		return LUTIL_PASSWD_ERR;	/* passwd must behave like a string */
   1209      1.1     lukem 	}
   1210      1.1     lukem 
   1211      1.1     lukem 	if( lutil_entropy( salt, sizeof( salt ) ) < 0 ) {
   1212      1.1     lukem 		return LUTIL_PASSWD_ERR;
   1213      1.1     lukem 	}
   1214      1.1     lukem 
   1215      1.1     lukem 	for( i=0; i< ( sizeof(salt) - 1 ); i++ ) {
   1216      1.1     lukem 		salt[i] = crypt64[ salt[i] % (sizeof(crypt64)-1) ];
   1217      1.1     lukem 	}
   1218      1.1     lukem 	salt[sizeof( salt ) - 1 ] = '\0';
   1219      1.1     lukem 
   1220      1.1     lukem 	if( salt_format != NULL ) {
   1221      1.1     lukem 		/* copy the salt we made into entropy before snprintfing
   1222      1.1     lukem 		   it back into the salt */
   1223      1.1     lukem 		char entropy[sizeof(salt)];
   1224      1.1     lukem 		strcpy( entropy, (char *) salt );
   1225      1.1     lukem 		snprintf( (char *) salt, sizeof(entropy), salt_format, entropy );
   1226      1.1     lukem 	}
   1227      1.1     lukem 
   1228      1.1     lukem 	rc = lutil_cryptptr( passwd->bv_val, (char *) salt, &hash->bv_val );
   1229      1.1     lukem 	if ( rc != LUTIL_PASSWD_OK ) return rc;
   1230      1.1     lukem 
   1231      1.1     lukem 	if( hash->bv_val == NULL ) return -1;
   1232      1.1     lukem 
   1233      1.1     lukem 	hash->bv_len = strlen( hash->bv_val );
   1234      1.1     lukem 
   1235      1.1     lukem 	save = hash->bv_val;
   1236      1.1     lukem 
   1237      1.1     lukem 	if( hash->bv_len == 0 ) {
   1238      1.1     lukem 		rc = LUTIL_PASSWD_ERR;
   1239      1.1     lukem 	} else {
   1240      1.1     lukem 		rc = pw_string( scheme, hash );
   1241      1.1     lukem 	}
   1242      1.1     lukem 	ber_memfree( save );
   1243      1.1     lukem 	return rc;
   1244      1.1     lukem }
   1245      1.1     lukem #endif
   1246      1.1     lukem 
   1247      1.1     lukem int lutil_salt_format(const char *format)
   1248      1.1     lukem {
   1249      1.1     lukem #ifdef SLAPD_CRYPT
   1250      1.2     lukem 	ber_memfree( salt_format );
   1251      1.1     lukem 
   1252      1.1     lukem 	salt_format = format != NULL ? ber_strdup( format ) : NULL;
   1253      1.1     lukem #endif
   1254      1.1     lukem 
   1255      1.1     lukem 	return 0;
   1256      1.1     lukem }
   1257      1.1     lukem 
   1258      1.1     lukem #ifdef SLAPD_CLEARTEXT
   1259      1.1     lukem static int hash_clear(
   1260      1.1     lukem 	const struct berval *scheme,
   1261      1.1     lukem 	const struct berval  *passwd,
   1262      1.1     lukem 	struct berval *hash,
   1263      1.1     lukem 	const char **text )
   1264      1.1     lukem {
   1265      1.1     lukem 	ber_dupbv( hash, (struct berval *)passwd );
   1266      1.1     lukem 	return LUTIL_PASSWD_OK;
   1267      1.1     lukem }
   1268      1.1     lukem #endif
   1269      1.1     lukem 
   1270