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