diff options
| author | cvsadm <cvsadm> | 2005-01-21 00:44:34 +0000 |
|---|---|---|
| committer | cvsadm <cvsadm> | 2005-01-21 00:44:34 +0000 |
| commit | b2093e3016027d6b5cf06b3f91f30769bfc099e2 (patch) | |
| tree | cf58939393a9032182c4fbc4441164a9456e82f8 /ldap/servers/plugins/pwdstorage | |
| download | ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.gz ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.xz ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.zip | |
Moving NSCP Directory Server from DirectoryBranch to TRUNK, initial drop. (foxworth)ldapserver7x
Diffstat (limited to 'ldap/servers/plugins/pwdstorage')
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/Makefile | 115 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/clear_pwd.c | 27 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/crypt_pwd.c | 91 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/dllmain.c | 91 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/libpwdstorage.def | 24 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/md5.h | 63 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/md5c.c | 337 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.bu | 405 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.c | 81 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/pwd_init.c | 146 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/pwdstorage.h | 99 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/sha_pwd.c | 111 | ||||
| -rw-r--r-- | ldap/servers/plugins/pwdstorage/ssha_pwd.c | 112 |
13 files changed, 1702 insertions, 0 deletions
diff --git a/ldap/servers/plugins/pwdstorage/Makefile b/ldap/servers/plugins/pwdstorage/Makefile new file mode 100644 index 00000000..efad0788 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/Makefile @@ -0,0 +1,115 @@ +# +# BEGIN COPYRIGHT BLOCK +# Copyright 2001 Sun Microsystems, Inc. +# Portions copyright 1999, 2001-2003 Netscape Communications Corporation. +# All rights reserved. +# END COPYRIGHT BLOCK +# +# +# GNU Makefile for Directory Server password_storaged-plugin.so password storage scheme plugins +# + +LDAP_SRC = ../../.. +MCOM_ROOT = ../../../../.. + +NOSTDCLEAN=true # don't let nsconfig.mk define target clean +NOSTDSTRIP=true # don't let nsconfig.mk define target strip +NSPR20=true # probably should be defined somewhere else (not sure where) + +OBJDEST = $(OBJDIR)/lib/libpwdstorage +LIBDIR = $(LIB_RELDIR) + +include $(MCOM_ROOT)/ldapserver/nsconfig.mk +include $(LDAP_SRC)/nsldap.mk + +ifeq ($(ARCH), WINNT) +DEF_FILE:=./libpwdstorage.def +endif + +CFLAGS+=$(SLCFLAGS) + +INCLUDES += -I$(LDAP_SRC)/servers/slapd + +PWD_OBJS= \ + pwd_init.o \ + clear_pwd.o \ + crypt_pwd.o \ + ns-mta-md5_pwd.o \ + sha_pwd.o \ + ssha_pwd.o \ + md5c.o + + +OBJS = $(addprefix $(OBJDEST)/, $(PWD_OBJS)) + +ifeq ($(ARCH), WINNT) +LIBPWD_DLL_OBJ = $(addprefix $(OBJDEST)/, dllmain.o) +endif + +LIBPWD = $(addprefix $(LIBDIR)/, $(PWD_DLL).$(DLL_SUFFIX)) + +ifeq ($(ARCH), WINNT) +EXTRA_LIBS_DEP += \ + $(LIBSLAPD_DEP) \ + $(LDAP_LIBUTIL_DEP) \ + $(LDAP_COMMON_LIBS_DEP) +EXTRA_LIBS_DEP += \ + $(LDAPSDK_DEP) \ + $(SECURITY_DEP) +EXTRA_LIBS += \ + $(LIBSLAPD) \ + $(LDAP_SDK_LIBLDAP_DLL) \ + $(LIBUTIL) \ + $(NSPRLINK) \ + $(LDAP_COMMON_LIBS) \ + $(SECURITYLINK) +endif +ifeq ($(ARCH), AIX) +EXTRA_LIBS_DEP += \ + $(LIBSLAPD_DEP) \ + $(LDAP_LIBUTIL_DEP) \ + $(LDAP_COMMON_LIBS_DEP) +EXTRA_LIBS_DEP += \ + $(LDAPSDK_DEP) \ + $(SECURITY_DEP) +EXTRA_LIBS += \ + $(LIBSLAPDLINK) \ + $(LDAP_SDK_LIBLDAP_DLL) \ + $(LIBUTIL) \ + $(NSPRLINK) \ + $(LDAP_COMMON_LIBS) \ + $(SECURITYLINK) +endif + +ifeq ($(ARCH), HPUX) +EXTRA_LIBS_DEP += $(LIBSLAPD_DEP) $(LDAPSDK_DEP) $(NSPR_DEP) $(SECURITY_DEP) +EXTRA_LIBS += $(DYN_NSHTTPD) $(ADMINUTIL_LINK) $(LDAPLINK) $(SECURITYLINK) $(NSPRLINK) $(ICULINK) +endif + +ifeq ($(ARCH), WINNT) +DLL_LDFLAGS += -def:"./libpwdstorage.def" +CFLAGS+= /WX +endif # WINNT + +ifeq ($(ARCH), AIX) +LD=ld +endif + +clientSDK: + +all: $(OBJDEST) $(LIBDIR) $(LIBPWD) + +$(LIBPWD): $(OBJS) $(LIBPWD_DLL_OBJ) $(DEF_FILE) + $(LINK_DLL) $(LIBPWD_DLL_OBJ) $(EXTRA_LIBS) + +veryclean: clean + +clean: + $(RM) $(OBJS) +ifeq ($(ARCH), WINNT) + $(RM) $(LIBPWD_DLL_OBJ) +endif + $(RM) $(LIBPWD) + +$(OBJDEST): + $(MKDIR) $(OBJDEST) diff --git a/ldap/servers/plugins/pwdstorage/clear_pwd.c b/ldap/servers/plugins/pwdstorage/clear_pwd.c new file mode 100644 index 00000000..4b2a3ca5 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/clear_pwd.c @@ -0,0 +1,27 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * slapd hashed password routines + * + */ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "pwdstorage.h" + +int +clear_pw_cmp( char *userpwd, char *dbpwd ) +{ + return( strcmp( userpwd, dbpwd )); +} + +char * +clear_pw_enc( char *pwd ) +{ + return( slapi_ch_strdup( pwd )); +} diff --git a/ldap/servers/plugins/pwdstorage/crypt_pwd.c b/ldap/servers/plugins/pwdstorage/crypt_pwd.c new file mode 100644 index 00000000..df179ef6 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/crypt_pwd.c @@ -0,0 +1,91 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * slapd hashed password routines + * + */ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#ifdef _WIN32 +char *crypt(char *key, char *salt); +#else +#include <sys/socket.h> +#if defined( hpux ) || defined ( AIX ) || defined (LINUX) || defined (OSF1) +#define __USE_XOPEN /* linux */ +#include <unistd.h> +#else /* hpux */ +#include <crypt.h> +#endif /* hpux */ +#endif /* _WIN32 */ + +#include "pwdstorage.h" + +static PRLock *cryptlock; /* Some implementations of crypt are not thread safe. ie. ours & Irix */ + +/* characters used in crypt encoding */ +static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + + +void +crypt_init() +{ + cryptlock = PR_NewLock(); +} + +int +crypt_pw_cmp( char *userpwd, char *dbpwd ) +{ + int rc; + char *cp; + PR_Lock(cryptlock); + /* we use salt (first 2 chars) of encoded password in call to crypt() */ + cp = crypt( userpwd, dbpwd ); + if (cp) { + rc= strcmp( dbpwd, cp); + } else { + rc = -1; + } + PR_Unlock(cryptlock); + return rc; +} + +char * +crypt_pw_enc( char *pwd ) +{ + char *cry, salt[3]; + char *enc= NULL; + long v; + static unsigned int seed = 0; + + if ( seed == 0) + { + seed = (unsigned int)slapi_rand(); + } + v = slapi_rand_r(&seed); + + salt[0] = itoa64[v & 0x3f]; + v >>= 6; + salt[1] = itoa64[v & 0x3f]; + salt[2] = '\0'; + + PR_Lock(cryptlock); + cry = crypt( pwd, salt ); + if ( cry != NULL ) + { + enc = slapi_ch_malloc( 3 + CRYPT_NAME_LEN + strlen( cry )); + if ( enc != NULL ) + { + sprintf( enc, "%c%s%c%s", PWD_HASH_PREFIX_START, CRYPT_SCHEME_NAME, PWD_HASH_PREFIX_END, cry ); + } + } + PR_Unlock(cryptlock); + return( enc ); +} + diff --git a/ldap/servers/plugins/pwdstorage/dllmain.c b/ldap/servers/plugins/pwdstorage/dllmain.c new file mode 100644 index 00000000..71530805 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/dllmain.c @@ -0,0 +1,91 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ + + /* + * Microsoft Windows specifics for LIBPWDSTORAGE DLL + */ +#include "ldap.h" + + +#ifdef _WIN32 +/* Lifted from Q125688 + * How to Port a 16-bit DLL to a Win32 DLL + * on the MSVC 4.0 CD + */ +BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved) +{ + + switch (fdwReason) + { + case DLL_PROCESS_ATTACH: + /* Code from LibMain inserted here. Return TRUE to keep the + DLL loaded or return FALSE to fail loading the DLL. + + You may have to modify the code in your original LibMain to + account for the fact that it may be called more than once. + You will get one DLL_PROCESS_ATTACH for each process that + loads the DLL. This is different from LibMain which gets + called only once when the DLL is loaded. The only time this + is critical is when you are using shared data sections. + If you are using shared data sections for statically + allocated data, you will need to be careful to initialize it + only once. Check your code carefully. + + Certain one-time initializations may now need to be done for + each process that attaches. You may also not need code from + your original LibMain because the operating system may now + be doing it for you. + */ + /* + * 16 bit code calls UnlockData() + * which is mapped to UnlockSegment in windows.h + * in 32 bit world UnlockData is not defined anywhere + * UnlockSegment is mapped to GlobalUnfix in winbase.h + * and the docs for both UnlockSegment and GlobalUnfix say + * ".. function is oboslete. Segments have no meaning + * in the 32-bit environment". So we do nothing here. + */ + + break; + + case DLL_THREAD_ATTACH: + /* Called each time a thread is created in a process that has + already loaded (attached to) this DLL. Does not get called + for each thread that exists in the process before it loaded + the DLL. + + Do thread-specific initialization here. + */ + break; + + case DLL_THREAD_DETACH: + /* Same as above, but called when a thread in the process + exits. + + Do thread-specific cleanup here. + */ + break; + + case DLL_PROCESS_DETACH: + /* Code from _WEP inserted here. This code may (like the + LibMain) not be necessary. Check to make certain that the + operating system is not doing it for you. + */ + + break; + } + /* The return value is only used for DLL_PROCESS_ATTACH; all other + conditions are ignored. */ + return TRUE; /* successful DLL_PROCESS_ATTACH */ +} +#else +int CALLBACK +LibMain( HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine ) +{ + /*UnlockData( 0 );*/ + return( 1 ); +} +#endif diff --git a/ldap/servers/plugins/pwdstorage/libpwdstorage.def b/ldap/servers/plugins/pwdstorage/libpwdstorage.def new file mode 100644 index 00000000..e19305d5 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/libpwdstorage.def @@ -0,0 +1,24 @@ +; BEGIN COPYRIGHT BLOCK +; Copyright 2001 Sun Microsystems, Inc. +; Portions copyright 1999, 2001-2003 Netscape Communications Corporation. +; All rights reserved. +; END COPYRIGHT BLOCK +; +DESCRIPTION 'Netscape Directory Server 7 password storage scheme Plugin' +EXPORTS + sha_pwd_storage_scheme_init @2 + ssha_pwd_storage_scheme_init @3 + crypt_pwd_storage_scheme_init @4 + clear_pwd_storage_scheme_init @5 + ns_mta_md5_pwd_storage_scheme_init @6 + clear_pw_cmp @7 + crypt_pw_cmp @8 + ns_mta_md5_pw_cmp @9 + sha1_pw_cmp @10 + sha1_pw_enc @11 + salted_sha1_pw_enc @12 + crypt_pw_enc @13 + clear_pw_enc @14 + mta_MD5Init @15 + mta_MD5Update @16 + mta_MD5Final @17 diff --git a/ldap/servers/plugins/pwdstorage/md5.h b/ldap/servers/plugins/pwdstorage/md5.h new file mode 100644 index 00000000..6f7ec036 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/md5.h @@ -0,0 +1,63 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * MD5 algorithm used by Netscape Mail Server + */ + +/* MD5 code taken from reference implementation published in RFC 1321 */ + +#ifndef _RFC1321_MD5_H_ +#define _RFC1321_MD5_H_ + +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + rights reserved. + + License to copy and use this software is granted provided that it + is identified as the "RSA Data Security, Inc. MD5 Message-Digest + Algorithm" in all material mentioning or referencing this software + or this function. + + License is also granted to make and use derivative works provided + that such works are identified as "derived from the RSA Data + Security, Inc. MD5 Message-Digest Algorithm" in all material + mentioning or referencing the derived work. + + RSA Data Security, Inc. makes no representations concerning either + the merchantability of this software or the suitability of this + software for any particular purpose. It is provided "as is" + without express or implied warranty of any kind. + + These notices must be retained in any copies of any part of this + documentation and/or software. + */ + +#include "nspr.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef unsigned char * POINTER; +typedef PRUint16 UINT2; +typedef PRUint32 UINT4; + +/* MD5 context. */ +typedef struct { + UINT4 state[4]; /* state (ABCD) */ + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ +} mta_MD5_CTX; + +void mta_MD5Init (mta_MD5_CTX *); +void mta_MD5Update (mta_MD5_CTX *, const unsigned char *, unsigned int); +void mta_MD5Final (unsigned char [16], mta_MD5_CTX *); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* end of _RFC1321_MD5_H_ */ + diff --git a/ldap/servers/plugins/pwdstorage/md5c.c b/ldap/servers/plugins/pwdstorage/md5c.c new file mode 100644 index 00000000..d78b772c --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/md5c.c @@ -0,0 +1,337 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* MD5 code taken from reference implementation published in RFC 1321 */ + +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm */ + +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + rights reserved. + + License to copy and use this software is granted provided that it + is identified as the "RSA Data Security, Inc. MD5 Message-Digest + Algorithm" in all material mentioning or referencing this software + or this function. + + License is also granted to make and use derivative works provided + that such works are identified as "derived from the RSA Data + Security, Inc. MD5 Message-Digest Algorithm" in all material + mentioning or referencing the derived work. + + RSA Data Security, Inc. makes no representations concerning either + the merchantability of this software or the suitability of this + software for any particular purpose. It is provided "as is" + without express or implied warranty of any kind. + + These notices must be retained in any copies of any part of this + documentation and/or software. + */ + +#include "md5.h" + +/* Constants for MD5Transform routine. */ + +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + +static void MD5Transform (UINT4 [4], const unsigned char [64]); +static void Encode (unsigned char *, const UINT4 *, unsigned int); +static void Decode (UINT4 *, const unsigned char *, unsigned int); +static void MD5_memcpy (POINTER, const POINTER, unsigned int); +static void MD5_memset (POINTER, int, unsigned int); + +static unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G, H and I are basic MD5 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~(x)) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~(z)))) + +/* ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. +Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } + +/* MD5 initialization. Begins an MD5 operation, writing a new context. + */ +void mta_MD5Init (context) +mta_MD5_CTX *context; /* context */ +{ + context->count[0] = context->count[1] = 0; + /* Load magic initialization constants. +*/ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* MD5 block update operation. Continues an MD5 message-digest + operation, processing another message block, and updating the + context. + */ +void mta_MD5Update (context, input, inputLen) +mta_MD5_CTX *context; /* context */ +const unsigned char *input; /* input block */ +unsigned int inputLen; /* length of input block */ +{ + unsigned int i, index, partLen; + + /* Compute number of bytes mod 64 */ + index = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - index; + + /* Transform as many times as possible. +*/ + if (inputLen >= partLen) { + MD5_memcpy + ((POINTER)&context->buffer[index], (POINTER)input, partLen); + MD5Transform (context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform (context->state, &input[i]); + + index = 0; + } + else + i = 0; + + /* Buffer remaining input */ + MD5_memcpy + ((POINTER)&context->buffer[index], (POINTER)&input[i], + inputLen-i); +} + +/* MD5 finalization. Ends an MD5 message-digest operation, writing the + the message digest and zeroizing the context. + */ +void mta_MD5Final (digest, context) +unsigned char digest[16]; /* message digest */ +mta_MD5_CTX *context; /* context */ +{ + unsigned char bits[8]; + unsigned int index, padLen; + + /* Save number of bits */ + Encode (bits, context->count, 8); + + /* Pad out to 56 mod 64. +*/ + index = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + mta_MD5Update (context, PADDING, padLen); + + /* Append length (before padding) */ + mta_MD5Update (context, bits, 8); + + /* Store state in digest */ + Encode (digest, context->state, 16); + + /* Zeroize sensitive information. +*/ + MD5_memset ((POINTER)context, 0, sizeof (*context)); +} + +/* MD5 basic transformation. Transforms state based on block. + */ +static void MD5Transform (state, block) +UINT4 state[4]; +const unsigned char block[64]; +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode (x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. +*/ + MD5_memset ((POINTER)x, 0, sizeof (x)); +} + +/* Encodes input (UINT4) into output (unsigned char). Assumes len is + a multiple of 4. + */ +static void Encode (output, input, len) +unsigned char *output; +const UINT4 *input; +unsigned int len; +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* Decodes input (unsigned char) into output (UINT4). Assumes len is + a multiple of 4. + */ +static void Decode (output, input, len) +UINT4 *output; +const unsigned char *input; +unsigned int len; +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +/* Note: Replace "for loop" with standard memcpy if possible. + */ + +static void MD5_memcpy (output, input, len) +POINTER output; +const POINTER input; +unsigned int len; +{ + unsigned int i; + + for (i = 0; i < len; i++) + output[i] = input[i]; +} + +/* Note: Replace "for loop" with standard memset if possible. + */ +static void MD5_memset (output, value, len) +POINTER output; +int value; +unsigned int len; +{ + unsigned int i; + + for (i = 0; i < len; i++) + ((char *)output)[i] = (char)value; +} diff --git a/ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.bu b/ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.bu new file mode 100644 index 00000000..7cdd74b3 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.bu @@ -0,0 +1,405 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * slapd hashed password routines + */ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "pwd.h" + + +/* + * Netscape Mail Server MD5 support (compare-only; no support for encoding) + */ + +static char * ns_mta_hextab = "0123456789abcdef"; + +static void +ns_mta_hexify(char *buffer, char *str, int len) +{ + char *pch = str; + char ch; + int i; + + for(i = 0;i < len; i ++) { + ch = pch[i]; + buffer[2*i] = ns_mta_hextab[(ch>>4)&15]; + buffer[2*i+1] = ns_mta_hextab[ch&15]; + } + + return; +} + +static char * +ns_mta_hash_alg(char *buffer, char *salt, char *passwd) +{ + mta_MD5_CTX context; + char saltstr[2048]; + unsigned char digest[16]; + + sprintf(saltstr,"%s%c%s%c%s",salt,89,passwd,247,salt); + + mta_MD5Init(&context); + mta_MD5Update(&context,(unsigned char *)saltstr,strlen(saltstr)); + mta_MD5Final(digest,&context); + ns_mta_hexify(buffer,(char*)digest,16); + buffer[32] = '\0'; + return(buffer); + +} + +int +ns_mta_md5_pw_cmp(char * clear, char *mangled) +{ + char mta_hash[33]; + char mta_salt[33]; + char buffer[65]; + + strncpy(mta_hash,mangled,32); + strncpy(mta_salt,&mangled[32],32); + + mta_hash[32] = mta_salt[32] = 0; + + return( strcmp(mta_hash,ns_mta_hash_alg(buffer,mta_salt,clear))); +} + + +/* MD5 code taken from reference implementation published in RFC 1321 */ + +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm */ + +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + rights reserved. + + License to copy and use this software is granted provided that it + is identified as the "RSA Data Security, Inc. MD5 Message-Digest + Algorithm" in all material mentioning or referencing this software + or this function. + + License is also granted to make and use derivative works provided + that such works are identified as "derived from the RSA Data + Security, Inc. MD5 Message-Digest Algorithm" in all material + mentioning or referencing the derived work. + + RSA Data Security, Inc. makes no representations concerning either + the merchantability of this software or the suitability of this + software for any particular purpose. It is provided "as is" + without express or implied warranty of any kind. + + These notices must be retained in any copies of any part of this + documentation and/or software. + */ + +#include "pw.h" + +/* Constants for MD5Transform routine. */ + +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + +static void MD5Transform (UINT4 [4], const unsigned char [64]); +static void Encode (unsigned char *, const UINT4 *, unsigned int); +static void Decode (UINT4 *, const unsigned char *, unsigned int); +static void MD5_memcpy (POINTER, const POINTER, unsigned int); +static void MD5_memset (POINTER, int, unsigned int); + +static unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G, H and I are basic MD5 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~(x)) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~(z)))) + +/* ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. +Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } + +/* MD5 initialization. Begins an MD5 operation, writing a new context. + */ +void mta_MD5Init (context) +mta_MD5_CTX *context; /* context */ +{ + context->count[0] = context->count[1] = 0; + /* Load magic initialization constants. +*/ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* MD5 block update operation. Continues an MD5 message-digest + operation, processing another message block, and updating the + context. + */ +void mta_MD5Update (context, input, inputLen) +mta_MD5_CTX *context; /* context */ +const unsigned char *input; /* input block */ +unsigned int inputLen; /* length of input block */ +{ + unsigned int i, index, partLen; + + /* Compute number of bytes mod 64 */ + index = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - index; + + /* Transform as many times as possible. +*/ + if (inputLen >= partLen) { + MD5_memcpy + ((POINTER)&context->buffer[index], (POINTER)input, partLen); + MD5Transform (context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform (context->state, &input[i]); + + index = 0; + } + else + i = 0; + + /* Buffer remaining input */ + MD5_memcpy + ((POINTER)&context->buffer[index], (POINTER)&input[i], + inputLen-i); +} + +/* MD5 finalization. Ends an MD5 message-digest operation, writing the + the message digest and zeroizing the context. + */ +void mta_MD5Final (digest, context) +unsigned char digest[16]; /* message digest */ +mta_MD5_CTX *context; /* context */ +{ + unsigned char bits[8]; + unsigned int index, padLen; + + /* Save number of bits */ + Encode (bits, context->count, 8); + + /* Pad out to 56 mod 64. +*/ + index = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + mta_MD5Update (context, PADDING, padLen); + + /* Append length (before padding) */ + mta_MD5Update (context, bits, 8); + + /* Store state in digest */ + Encode (digest, context->state, 16); + + /* Zeroize sensitive information. +*/ + MD5_memset ((POINTER)context, 0, sizeof (*context)); +} + +/* MD5 basic transformation. Transforms state based on block. + */ +static void MD5Transform (state, block) +UINT4 state[4]; +const unsigned char block[64]; +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode (x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. +*/ + MD5_memset ((POINTER)x, 0, sizeof (x)); +} + +/* Encodes input (UINT4) into output (unsigned char). Assumes len is + a multiple of 4. + */ +static void Encode (output, input, len) +unsigned char *output; +const UINT4 *input; +unsigned int len; +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* Decodes input (unsigned char) into output (UINT4). Assumes len is + a multiple of 4. + */ +static void Decode (output, input, len) +UINT4 *output; +const unsigned char *input; +unsigned int len; +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +/* Note: Replace "for loop" with standard memcpy if possible. + */ + +static void MD5_memcpy (output, input, len) +POINTER output; +const POINTER input; +unsigned int len; +{ + unsigned int i; + + for (i = 0; i < len; i++) + output[i] = input[i]; +} + +/* Note: Replace "for loop" with standard memset if possible. + */ +static void MD5_memset (output, value, len) +POINTER output; +int value; +unsigned int len; +{ + unsigned int i; + + for (i = 0; i < len; i++) + ((char *)output)[i] = (char)value; +} + diff --git a/ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.c b/ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.c new file mode 100644 index 00000000..f3c11a10 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/ns-mta-md5_pwd.c @@ -0,0 +1,81 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * slapd hashed password routines + * + */ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "pwdstorage.h" + +#include "md5.h" /* JCM - This is a core server header... These functions could be made part of the slapi API. */ + + +/* + * Netscape Mail Server MD5 support (compare-only; no support for encoding) + */ + +static char * ns_mta_hextab = "0123456789abcdef"; + +static void +ns_mta_hexify(char *buffer, char *str, int len) +{ + char *pch = str; + char ch; + int i; + + for(i = 0;i < len; i ++) { + ch = pch[i]; + buffer[2*i] = ns_mta_hextab[(ch>>4)&15]; + buffer[2*i+1] = ns_mta_hextab[ch&15]; + } + + return; +} + +static char * +ns_mta_hash_alg(char *buffer, char *salt, char *passwd) +{ + mta_MD5_CTX context; + char *saltstr; + unsigned char digest[16]; + + + if ( (saltstr = slapi_ch_malloc(strlen(salt)*2 + strlen(passwd) + 3)) + == NULL ) { + return( NULL ); + } + + sprintf(saltstr,"%s%c%s%c%s",salt,89,passwd,247,salt); + + mta_MD5Init(&context); + mta_MD5Update(&context,(unsigned char *)saltstr,strlen(saltstr)); + mta_MD5Final(digest,&context); + ns_mta_hexify(buffer,(char*)digest,16); + buffer[32] = '\0'; + slapi_ch_free((void**)&saltstr); + return(buffer); + +} + +int +ns_mta_md5_pw_cmp(char * clear, char *mangled) +{ + char mta_hash[33]; + char mta_salt[33]; + char buffer[65]; + + strncpy(mta_hash,mangled,32); + strncpy(mta_salt,&mangled[32],32); + + mta_hash[32] = mta_salt[32] = 0; + + return( strcmp(mta_hash,ns_mta_hash_alg(buffer,mta_salt,clear))); +} + diff --git a/ldap/servers/plugins/pwdstorage/pwd_init.c b/ldap/servers/plugins/pwdstorage/pwd_init.c new file mode 100644 index 00000000..4ee5138b --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/pwd_init.c @@ -0,0 +1,146 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "pwdstorage.h" +#include "dirver.h" + +static Slapi_PluginDesc sha_pdesc = { "sha-password-storage-scheme", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT, "Secure Hashing Algorithm (SHA)" }; + +static Slapi_PluginDesc ssha_pdesc = { "ssha-password-storage-scheme", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT, "Salted Secure Hashing Algorithm (SSHA)" }; + +static Slapi_PluginDesc crypt_pdesc = { "crypt-password-storage-scheme", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT, "Unix crypt algorithm (CRYPT)" }; + +static Slapi_PluginDesc clear_pdesc = { "clear-password-storage-scheme", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT, "No encryption (CLEAR)" }; + +static Slapi_PluginDesc ns_mta_md5_pdesc = { "NS-MTA-MD5-password-storage-scheme", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT, "Netscape MD5 (NS-MTA-MD5)" }; + +static char *plugin_name = "NSPwdStoragePlugin"; + +int +sha_pwd_storage_scheme_init( Slapi_PBlock *pb ) +{ + int rc; + char *name; + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "=> sha_pwd_storage_scheme_init\n" ); + + rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, + (void *) SLAPI_PLUGIN_VERSION_01 ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, + (void *)&sha_pdesc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_ENC_FN, + (void *) sha1_pw_enc); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_CMP_FN, + (void *) sha1_pw_cmp ); + name = slapi_ch_strdup("SHA"); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_NAME, + name ); + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "<= sha_pwd_storage_scheme_init %d\n\n", rc ); + + return( rc ); +} + +int +ssha_pwd_storage_scheme_init( Slapi_PBlock *pb ) +{ + int rc; + char *name; + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "=> ssha_pwd_storage_scheme_init\n" ); + + rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, + (void *) SLAPI_PLUGIN_VERSION_01 ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, + (void *)&ssha_pdesc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_ENC_FN, + (void *) salted_sha1_pw_enc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_CMP_FN, + (void *) sha1_pw_cmp ); + name = slapi_ch_strdup("SSHA"); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_NAME, + name ); + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "<= ssha_pwd_storage_scheme_init %d\n\n", rc ); + return( rc ); +} + +int +crypt_pwd_storage_scheme_init( Slapi_PBlock *pb ) +{ + int rc; + char *name; + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "=> crypt_pwd_storage_scheme_init\n" ); + + crypt_init(); + rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, + (void *) SLAPI_PLUGIN_VERSION_01 ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, + (void *)&crypt_pdesc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_ENC_FN, + (void *) crypt_pw_enc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_CMP_FN, + (void *) crypt_pw_cmp ); + name = slapi_ch_strdup("CRYPT"); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_NAME, + name ); + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "<= crypt_pwd_storage_scheme_init %d\n\n", rc ); + return( rc ); +} + +int +clear_pwd_storage_scheme_init( Slapi_PBlock *pb ) +{ + int rc; + char *name; + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "=> clear_pwd_storage_scheme_init\n" ); + + rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, + (void *) SLAPI_PLUGIN_VERSION_01 ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, + (void *)&clear_pdesc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_ENC_FN, + (void *) clear_pw_enc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_CMP_FN, + (void *) clear_pw_cmp ); + name = slapi_ch_strdup("CLEAR"); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_NAME, + name ); + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "<= clear_pwd_storage_scheme_init %d\n\n", rc ); + return( rc ); +} + +int +ns_mta_md5_pwd_storage_scheme_init( Slapi_PBlock *pb ) +{ + int rc; + char *name; + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "=> ns_mta_md5_pwd_storage_scheme_init\n" ); + + rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, + (void *) SLAPI_PLUGIN_VERSION_01 ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, + (void *)&ns_mta_md5_pdesc ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_ENC_FN, + (void *) NULL ); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_CMP_FN, + (void *) ns_mta_md5_pw_cmp ); + name = slapi_ch_strdup("NS-MTA-MD5"); + rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_PWD_STORAGE_SCHEME_NAME, + name ); + + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "<= ns_mta_md5_pwd_storage_scheme_init %d\n\n", rc ); + return( rc ); +} diff --git a/ldap/servers/plugins/pwdstorage/pwdstorage.h b/ldap/servers/plugins/pwdstorage/pwdstorage.h new file mode 100644 index 00000000..0e938cb9 --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/pwdstorage.h @@ -0,0 +1,99 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +#ifndef _PWDSTORAGE_H +#define _PWDSTORAGE_H + +#include "slapi-plugin.h" +#include <ssl.h> +#include "nspr.h" +#include "ldif.h" +#include "md5.h" + +#include <dirlite_strings.h> /* PLUGIN_MAGIC_VENDOR_STR */ + +#define PWD_HASH_PREFIX_START '{' +#define PWD_HASH_PREFIX_END '}' + +#define SHA1_SCHEME_NAME "SHA" +#define SHA1_NAME_LEN 3 +#define SALTED_SHA1_SCHEME_NAME "SSHA" +#define SALTED_SHA1_NAME_LEN 4 +#define CRYPT_SCHEME_NAME "crypt" +#define CRYPT_NAME_LEN 5 +#define NS_MTA_MD5_SCHEME_NAME "NS-MTA-MD5" +#define NS_MTA_MD5_NAME_LEN 10 +#define CLEARTEXT_SCHEME_NAME "clear" +#define CLEARTEXT_NAME_LEN 5 + +SECStatus sha1_salted_hash(unsigned char *hash_out, char *pwd, struct berval *salt); +int sha1_pw_cmp( char *userpwd, char *dbpwd ); +char * sha1_pw_enc( char *pwd ); +char * salted_sha1_pw_enc( char *pwd ); +int clear_pw_cmp( char *userpwd, char *dbpwd ); +char *clear_pw_enc( char *pwd ); +void crypt_init(); +int crypt_pw_cmp( char *userpwd, char *dbpwd ); +char *crypt_pw_enc( char *pwd ); +int ns_mta_md5_pw_cmp( char *userpwd, char *dbpwd ); + + +#if !defined(NET_SSL) +/******************************************/ +/* + * Some of the stuff below depends on a definition for uint32, so + * we include one here. Other definitions appear in nspr/prtypes.h, + * at least. All the platforms we support use 32-bit ints. + */ +typedef unsigned int uint32; + + +/******************************************/ +/* + * The following is from ds.h, which the libsec sec.h stuff depends on (see + * comment below). + */ +/* +** A status code. Status's are used by procedures that return status +** values. Again the motivation is so that a compiler can generate +** warnings when return values are wrong. Correct testing of status codes: +** +** DSStatus rv; +** rv = some_function (some_argument); +** if (rv != DSSuccess) +** do_an_error_thing(); +** +*/ +typedef enum DSStatusEnum { + DSWouldBlock = -2, + DSFailure = -1, + DSSuccess = 0 +} DSStatus; + + +/******************************************/ +/* + * All of the SHA1-related defines are from libsec's "sec.h" -- including + * it directly pulls in way too much stuff that we conflict with. Ugh. + */ + +/* + * Number of bytes each hash algorithm produces + */ +#define SHA1_LENGTH 20 + +/******************************************/ +/* +** SHA-1 secure hash function +*/ + +/* +** Hash a null terminated string "src" into "dest" using SHA-1 +*/ +DSStatus SHA1_Hash(unsigned char *dest, char *src); + +#endif /* !defined(NET_SSL) */ + +#endif /* _PWDSTORAGE_H */ diff --git a/ldap/servers/plugins/pwdstorage/sha_pwd.c b/ldap/servers/plugins/pwdstorage/sha_pwd.c new file mode 100644 index 00000000..c8cf435d --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/sha_pwd.c @@ -0,0 +1,111 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * slapd hashed password routines + * + */ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "pwdstorage.h" + +#if defined(NET_SSL) +#include <sechash.h> +#endif /* NET_SSL */ + +#define SHA1_SALT_LENGTH 8 /* number of bytes of data in salt */ +#define NOT_FIRST_TIME (time_t)1 /* not the first logon */ + +static char *hasherrmsg = "pw_cmp: %s userPassword \"%s\" is the wrong length or is not properly encoded BASE64\n"; + +static char *plugin_name = "NSPwdStoragePlugin"; + +#define DS40B1_SALTED_SHA_LENGTH 18 +/* Directory Server 4.0 Beta 1 implemented a scheme that stored + * 8 bytes of salt plus the first 10 bytes of the SHA-1 digest. + * It's obsolescent now, but we still handle such stored values. + */ + +int +sha1_pw_cmp (char *userpwd, char *dbpwd ) +{ + /* + * SHA1 passwords are stored in the database as SHA1_LENGTH bytes of + * hash, followed by zero or more bytes of salt, all BASE64 encoded. + */ + int result = 1; /* failure */ + unsigned char userhash[SHA1_LENGTH]; + unsigned char quick_dbhash[SHA1_LENGTH + SHA1_SALT_LENGTH + 3]; + unsigned char *dbhash = quick_dbhash; + struct berval salt; + int hash_len; /* must be a signed valued -- see below */ + + /* + * Decode hash stored in database. + * + * Note that ldif_base64_decode() returns a value less than zero to + * indicate that a decoding error occurred, so it is critical that + * hash_len be a signed value. + */ + hash_len = (((strlen(dbpwd) + 3) / 4) * 3); /* maybe less */ + if ( hash_len > sizeof(quick_dbhash) ) { /* get more space: */ + dbhash = (unsigned char*) slapi_ch_malloc( hash_len ); + if ( dbhash == NULL ) goto loser; + } + hash_len = ldif_base64_decode( dbpwd, dbhash ); + if ( hash_len >= SHA1_LENGTH ) { + salt.bv_val = (void*)(dbhash + SHA1_LENGTH); + salt.bv_len = hash_len - SHA1_LENGTH; + } else if ( hash_len == DS40B1_SALTED_SHA_LENGTH ) { + salt.bv_val = (void*)dbhash; + salt.bv_len = 8; + } else { /* unsupported, invalid BASE64 (hash_len < 0), or similar */ + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, hasherrmsg, SHA1_SCHEME_NAME, dbpwd ); + goto loser; + } + + /* SHA1 hash the user's key */ + if ( sha1_salted_hash( userhash, userpwd, &salt ) != SECSuccess ) { + slapi_log_error( SLAPI_LOG_PLUGIN, plugin_name, "sha1_pw_cmp: SHA1_Hash() failed\n"); + goto loser; + } + + /* the proof is in the comparison... */ + result = ( hash_len == DS40B1_SALTED_SHA_LENGTH ) ? + ( memcmp( userhash, dbhash + 8, hash_len - 8 )) : + ( memcmp( userhash, dbhash, SHA1_LENGTH )); + + loser: + if ( dbhash && dbhash != quick_dbhash ) slapi_ch_free( (void**)&dbhash ); + return result; +} + + +char * +sha1_pw_enc( char *pwd ) +{ + unsigned char hash[ SHA1_LENGTH ]; + char *enc; + + /* SHA1 hash the user's key */ + if ( sha1_salted_hash( hash, pwd, NULL ) != SECSuccess ) { + return( NULL ); + } + + if (( enc = slapi_ch_malloc( 3 + SHA1_NAME_LEN + + LDIF_BASE64_LEN( SHA1_LENGTH ))) == NULL ) { + return( NULL ); + } + + sprintf( enc, "%c%s%c", PWD_HASH_PREFIX_START, SHA1_SCHEME_NAME, + PWD_HASH_PREFIX_END ); + (void)ldif_base64_encode( hash, enc + 2 + SHA1_NAME_LEN, + SHA1_LENGTH, -1 ); + + return( enc ); +} diff --git a/ldap/servers/plugins/pwdstorage/ssha_pwd.c b/ldap/servers/plugins/pwdstorage/ssha_pwd.c new file mode 100644 index 00000000..b3c82d6d --- /dev/null +++ b/ldap/servers/plugins/pwdstorage/ssha_pwd.c @@ -0,0 +1,112 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ +/* + * slapd hashed password routines + * + */ + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "pwdstorage.h" +#include "prtime.h" +#include "prlong.h" + +#if defined(NET_SSL) +#include <pk11func.h> +#include <pk11pqg.h> +#endif /* NET_SSL */ + +#define SHA1_SALT_LENGTH 8 /* number of bytes of data in salt */ + +static void ssha_rand_array(void *randx, size_t len); + + +/* *************************************************** + Identical function to slapi_rand_array in util.c, but can't use + that here since this module is included in libds_admin, which doesn't + link to libslapd. + *************************************************** */ +static void +ssha_rand_array(void *randx, size_t len) +{ + PK11_RandomUpdate(randx, len); + PK11_GenerateRandom((unsigned char *)randx, (int)len); +} + +/* + * A salted SHA1 hash + * if salt is null, no salt is used (this is for backward compatibility) +*/ +SECStatus +sha1_salted_hash(unsigned char *hash_out, char *pwd, struct berval *salt) +{ + PK11Context *ctx; + unsigned int outLen; + SECStatus rc; + + if (salt && salt->bv_len) { + ctx = PK11_CreateDigestContext(SEC_OID_SHA1); + if (ctx == NULL) { + rc = SECFailure; + } + else { + PK11_DigestBegin(ctx); + PK11_DigestOp(ctx, (unsigned char*)pwd, strlen(pwd)); + PK11_DigestOp(ctx, (unsigned char*)(salt->bv_val), salt->bv_len); + PK11_DigestFinal(ctx, hash_out, &outLen, SHA1_LENGTH); + PK11_DestroyContext(ctx, 1); + if (outLen == SHA1_LENGTH) + rc = SECSuccess; + else + rc = SECFailure; + } + } + else { + /*backward compatibility*/ + rc = PK11_HashBuf(SEC_OID_SHA1, hash_out, (unsigned char *)pwd, strlen(pwd)); + } + + return rc; +} + +char * +salted_sha1_pw_enc( char *pwd ) +{ + unsigned char hash[ SHA1_LENGTH + SHA1_SALT_LENGTH ]; + unsigned char *salt = hash + SHA1_LENGTH; + struct berval saltval; + char *enc; + + saltval.bv_val = (void*)salt; + saltval.bv_len = SHA1_SALT_LENGTH; + + /* generate a new random salt */ + /* Note: the uninitialized salt array provides a little extra entropy + * to the random array generation, but it is not really needed since + * PK11_GenerateRandom takes care of seeding. In any case, it doesn't + * hurt. */ + ssha_rand_array( salt, SHA1_SALT_LENGTH ); + + /* SHA1 hash the user's key */ + if ( sha1_salted_hash( hash, pwd, &saltval ) != SECSuccess ) { + return( NULL ); + } + + if (( enc = PR_Malloc( 3 + SALTED_SHA1_NAME_LEN + + LDIF_BASE64_LEN(sizeof(hash)))) == NULL ) { + return( NULL ); + } + + sprintf( enc, "%c%s%c", PWD_HASH_PREFIX_START, SALTED_SHA1_SCHEME_NAME, + PWD_HASH_PREFIX_END ); + (void)ldif_base64_encode( hash, enc + 2 + SALTED_SHA1_NAME_LEN, + sizeof(hash), -1 ); + + return( enc ); +} + |
