! netinet/in.h moved after sys/types.h in sha1.c to avoid compilation
author3APA3A <3APA3A>
Tue, 18 Jun 2002 16:17:32 +0000 (16:17 +0000)
committer3APA3A <3APA3A>
Tue, 18 Jun 2002 16:17:32 +0000 (16:17 +0000)
  errors under *BSD
+ smbencrypt utility added for LM-Password/NT-Password generation

src/modules/rlm_mschap/Makefile
src/modules/rlm_mschap/sha1.c
src/modules/rlm_mschap/smbencrypt.c [new file with mode: 0644]

index c0a5f43..4d17b80 100644 (file)
@@ -2,6 +2,8 @@ TARGET          = rlm_mschap
 SRCS           = rlm_mschap.c deskey.c desport.c md4c.c smbpass.c sha1.c
 HEADERS                = md4.h des.h smbpass.h sha1.h
 RLM_CFLAGS     =
+RLM_UTILS      = smbencrypt
+RLM_INSTALL    = smbencrypt
 
 # over-ride the previous assignment if we're not building anything
 include ../rules.mak
@@ -9,3 +11,6 @@ include ../rules.mak
 $(STATIC_OBJS): $(HEADERS)
 
 $(DYNAMIC_OBJS): $(HEADERS)
+
+smbencrypt: $(STATIC_OBJS)
+       $(CC) $(LDFLAGS) -osmbencrypt smbencrypt.c deskey.o desport.o md4c.o smbpass.o
\ No newline at end of file
index 24bd8fa..9971b8f 100644 (file)
@@ -6,9 +6,9 @@
  *  Version:   $Id$
  */
  
-#include <netinet/in.h>
 #include <string.h>
 #include <sys/types.h>
+#include <netinet/in.h>
 #include "sha1.h"
 
 #define blk0(i) (block->l[i] = htonl(block->l[i]))
diff --git a/src/modules/rlm_mschap/smbencrypt.c b/src/modules/rlm_mschap/smbencrypt.c
new file mode 100644 (file)
index 0000000..b0dc2fa
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+       smbencrypt - produces LM-Passowrd and NT-Password from
+       cleartext password
+       
+       (c) 2002 3APA3A for FreeRADIUS project
+ */
+
+#include       "../../include/autoconf.h"
+#include       "../../include/libradius.h"
+#include        <stdio.h>
+#include        <stdlib.h>
+#include        <string.h>
+#include        <ctype.h>
+
+#include        "des.h"
+#include        "md5.h"
+
+char * hex = "0123456789ABCDEF";
+
+void tohex (const unsigned char * src, size_t len, char *dst) {
+ int i;
+ for (i=0; i<len; i++) {
+       dst[(i*2)] = hex[(src[i]/16)];
+       dst[(i*2) + 1] = hex[(src[i]&0x0F)];
+ }
+ dst[(i*2)] = 0;
+}
+
+/* 
+ *     parity_key takes a 7-byte string in szIn and returns an
+ *     8-byte string in szOut.  It inserts a 1 into every 8th bit.
+ *     DES just strips these back out.
+ */
+static void parity_key(char * szOut, const char * szIn)
+{
+       int i;
+       unsigned char cNext = 0;
+       unsigned char cWorking = 0;
+       
+       for (i = 0; i < 7; i++) {
+               /* Shift operator works in place.  Copy the char out */
+               cWorking = szIn[i];
+               szOut[i] = (cWorking >> i) | cNext | 1;
+               cWorking = szIn[i];
+               cNext = (cWorking << (7 - i));
+       }
+       szOut[i] = cNext | 1;
+}
+
+
+/*
+ *     des_encrypt takes an 8-byte string and a 7-byte key and
+ *     returns an 8-byte DES encrypted string in szOut
+ */
+static void des_encrypt(const char *szClear, const char *szKey, char *szOut)
+{
+       char szParityKey[9];
+       unsigned long ulK[16][2];
+       
+       parity_key(szParityKey, szKey); /* Insert parity bits */
+       strncpy(szOut, szClear, 8);     /* des encrypts in place */
+       deskey(ulK, (unsigned char *) szParityKey, 0);  /* generate keypair */
+       des(ulK, szOut);  /* encrypt */
+}
+
+
+static void ntpwdhash (char *szHash, const char *szPassword)
+{
+       char szUnicodePass[513];
+       char nPasswordLen;
+       int i;
+
+       /*
+        *      NT passwords are unicode.  Convert plain text password
+        *      to unicode by inserting a zero every other byte
+        */
+       nPasswordLen = strlen(szPassword);
+       for (i = 0; i < nPasswordLen; i++) {
+               szUnicodePass[i << 1] = szPassword[i];
+               szUnicodePass[(i << 1) + 1] = 0;
+       }
+
+       /* Encrypt Unicode password to a 16-byte MD4 hash */
+       md4_calc(szHash, szUnicodePass, (nPasswordLen<<1) );
+}
+
+
+
+/*
+ *     lmpwdhash converts 14-byte null-padded uppercase OEM
+ *     password to 16-byte DES hash with predefined salt string
+ */
+static void lmpwdhash (char *szHash, const char *szPassword)
+{
+       char szOEMPass[14];
+       char stdText[] = "KGS!@#$%";
+       int i;
+
+       memset(szOEMPass, 0, 14);
+       for (i = 0; i < 14 && szPassword[i]; i++)
+               szOEMPass[i] = toupper(szPassword[i]);
+
+       /* Obtain DES hash of OEM password */
+       des_encrypt(stdText, szOEMPass, szHash); 
+       des_encrypt(stdText, szOEMPass+7, szHash+8);
+}
+
+int main (int argc, char *argv[]) {
+       int i, l;
+       char password[1024];
+       char hash[16];
+       char ntpass[33];
+       char lmpass[33];
+       
+       fprintf(stderr, "LM Hash                         \tNT Hash\n");
+       fprintf(stderr, "--------------------------------\t--------------------------------\n");
+       fflush(stderr);
+       for(i=1; i<argc; i++ ) {
+               l = strlen(password);
+               if (l & password[l-1] == '\n') password [l-1] = 0;
+               lmpwdhash (hash, argv[i]);
+               tohex (hash, 16, lmpass);
+               ntpwdhash (hash, argv[i]);
+               tohex (hash, 16, ntpass);
+               printf("%s\t%s\n", lmpass, ntpass);
+       }
+       return 0;
+}