]> Pileus Git - ~andy/fetchmail/blob - smbencrypt.c
Note Earl's regression fix for SSL_CTX_clear_options() on older OpenSSL.
[~andy/fetchmail] / smbencrypt.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB parameters and setup
5    Copyright (C) Andrew Tridgell 1992-1998
6    Modified by Jeremy Allison 1995.
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define DEBUG(a,b) ;
24
25 extern int DEBUGLEVEL;
26
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include "smbbyteorder.h"
32 #include "smbdes.h"
33 #include "smbencrypt.h"
34 #include "smbmd4.h"
35
36 #ifndef _AIX
37 typedef unsigned char uchar;
38 typedef signed short int16;
39 #endif
40 typedef int BOOL;
41 #define False 0
42 #define True  1
43
44 /****************************************************************************
45  Like strncpy but always null terminates. Make sure there is room!
46  The variable n should always be one less than the available size.
47 ****************************************************************************/
48
49 static char *StrnCpy(char *dest,const char *src, size_t n)
50 {
51   char *d = dest;
52   if (!dest) return(NULL);
53   if (!src) {
54     *dest = 0;
55     return(dest);
56   }
57   while (n-- && (*d++ = *src++)) ;
58   *d = 0;
59   return(dest);
60 }
61
62 static size_t skip_multibyte_char(char c)
63 {
64     (void)c;
65     return 0;
66 }
67
68
69 /*******************************************************************
70 safe string copy into a known length string. maxlength does not
71 include the terminating zero.
72 ********************************************************************/
73
74 static void strupper(char *s)
75 {
76 while (*s)
77   {
78     {
79     size_t skip = skip_multibyte_char( *s );
80     if( skip != 0 )
81       s += skip;
82     else
83       {
84       if (islower((unsigned char)*s))
85         *s = toupper((unsigned char)*s);
86       s++;
87       }
88     }
89   }
90 }
91
92 extern void SMBOWFencrypt(uchar passwd[16], uchar *c8, uchar p24[24]);
93
94 /*
95  This implements the X/Open SMB password encryption
96  It takes a password, a 8 byte "crypt key" and puts 24 bytes of 
97  encrypted password into p24 
98  */
99
100 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
101   {
102   uchar p14[15], p21[21];
103   
104   memset(p21,'\0',21);
105   memset(p14,'\0',14);
106   StrnCpy((char *)p14,(char *)passwd,14);
107   
108   strupper((char *)p14);
109   E_P16(p14, p21); 
110   
111   SMBOWFencrypt(p21, c8, p24);
112   
113 #ifdef DEBUG_PASSWORD
114   DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
115   dump_data(100, (char *)p21, 16);
116   dump_data(100, (char *)c8, 8);
117   dump_data(100, (char *)p24, 24);
118 #endif
119   }
120
121 /* Routines for Windows NT MD4 Hash functions. */
122 static int _my_wcslen(int16 *str)
123 {
124         int len = 0;
125         while(*str++ != 0)
126                 len++;
127         return len;
128 }
129
130 /*
131  * Convert a string into an NT UNICODE string.
132  * Note that regardless of processor type 
133  * this must be in intel (little-endian)
134  * format.
135  */
136  
137 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
138 {
139         int i;
140         int16 val;
141  
142         for(i = 0; i < len; i++) {
143                 val = *src;
144                 SSVAL(dst,0,val);
145                 dst++;
146                 src++;
147                 if(val == 0)
148                         break;
149         }
150         return i;
151 }
152
153 /* 
154  * Creates the MD4 Hash of the users password in NT UNICODE.
155  */
156  
157 static void E_md4hash(uchar *passwd, uchar *p16)
158 {
159         int len;
160         int16 wpwd[129];
161         
162         /* Password cannot be longer than 128 characters */
163         len = strlen((char *)passwd);
164         if(len > 128)
165                 len = 128;
166         /* Password must be converted to NT unicode */
167         _my_mbstowcs(wpwd, passwd, len);
168         wpwd[len] = 0; /* Ensure string is null terminated */
169         /* Calculate length in bytes */
170         len = _my_wcslen(wpwd) * sizeof(int16);
171
172         mdfour(p16, (unsigned char *)wpwd, len);
173 }
174
175 /* Does the des encryption from the NT or LM MD4 hash. */
176 void SMBOWFencrypt(uchar passwd[16], uchar *c8, uchar p24[24])
177 {
178         uchar p21[21];
179  
180         memset(p21,'\0',21);
181  
182         memcpy(p21, passwd, 16);    
183         E_P24(p21, c8, p24);
184 }
185
186 /* Does the NT MD4 hash then des encryption. */
187  
188 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
189 {
190         uchar p21[21];
191  
192         memset(p21,'\0',21);
193  
194         E_md4hash(passwd, p21);    
195         SMBOWFencrypt(p21, c8, p24);
196
197 #ifdef DEBUG_PASSWORD
198         DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
199         dump_data(100, (char *)p21, 16);
200         dump_data(100, (char *)c8, 8);
201         dump_data(100, (char *)p24, 24);
202 #endif
203 }
204
205 #if 0
206
207 BOOL make_oem_passwd_hash(char data[516], const char *passwd, uchar old_pw_hash[16], BOOL unicode)
208 {
209         int new_pw_len = strlen(passwd) * (unicode ? 2 : 1);
210
211         if (new_pw_len > 512)
212         {
213                 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
214                 return False;
215         }
216
217         /*
218          * Now setup the data area.
219          * We need to generate a random fill
220          * for this area to make it harder to
221          * decrypt. JRA.
222          */
223         generate_random_buffer((unsigned char *)data, 516, False);
224         if (unicode)
225         {
226                 struni2( &data[512 - new_pw_len], passwd);
227         }
228         else
229         {
230                 fstrcpy( &data[512 - new_pw_len], passwd);
231         }
232         SIVAL(data, 512, new_pw_len);
233
234 #ifdef DEBUG_PASSWORD
235         DEBUG(100,("make_oem_passwd_hash\n"));
236         dump_data(100, data, 516);
237 #endif
238         SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, True);
239
240         return True;
241 }
242
243 #endif