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