Updates!
[tprouty/samba.git] / source / libsmb / smbencrypt.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup
4    Copyright (C) Andrew Tridgell 1992-1998
5    Modified by Jeremy Allison 1995.
6    Copyright (C) Jeremy Allison 1995-2000.
7    Copyright (C) Luke Kennethc Casson Leighton 1996-2000.
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "byteorder.h"
26
27 /*
28    This implements the X/Open SMB password encryption
29    It takes a password ('unix' string), a 8 byte "crypt key" 
30    and puts 24 bytes of encrypted password into p24 */
31 void SMBencrypt(const char *passwd, const uchar *c8, uchar p24[24])
32 {
33         uchar p21[21];
34
35         memset(p21,'\0',21);
36         E_deshash(passwd, p21); 
37
38         SMBOWFencrypt(p21, c8, p24);
39
40 #ifdef DEBUG_PASSWORD
41         DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
42         dump_data(100, (char *)p21, 16);
43         dump_data(100, (const char *)c8, 8);
44         dump_data(100, (char *)p24, 24);
45 #endif
46 }
47
48 /**
49  * Creates the MD4 Hash of the users password in NT UNICODE.
50  * @param passwd password in 'unix' charset.
51  * @param p16 return password hashed with md4, caller allocated 16 byte buffer
52  */
53  
54 void E_md4hash(const char *passwd, uchar p16[16])
55 {
56         int len;
57         smb_ucs2_t wpwd[129];
58         
59         /* Password must be converted to NT unicode - null terminated. */
60         push_ucs2(NULL, wpwd, (const char *)passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);
61         /* Calculate length in bytes */
62         len = strlen_w(wpwd) * sizeof(int16);
63
64         mdfour(p16, (unsigned char *)wpwd, len);
65         ZERO_STRUCT(wpwd);      
66 }
67
68 /**
69  * Creates the DES forward-only Hash of the users password in DOS ASCII charset
70  * @param passwd password in 'unix' charset.
71  * @param p16 return password hashed with DES, caller allocated 16 byte buffer
72  */
73  
74 void E_deshash(const char *passwd, uchar p16[16])
75 {
76         uchar dospwd[15]; /* Password must not be > 14 chars long. */
77         ZERO_STRUCT(dospwd);
78         ZERO_STRUCTP(p16);
79         
80         /* Password must be converted to DOS charset - null terminated, uppercase. */
81         push_ascii(dospwd, (const char *)passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);
82
83         E_P16(dospwd, p16);
84
85         ZERO_STRUCT(dospwd);    
86 }
87
88 /**
89  * Creates the MD4 and DES (LM) Hash of the users password.  
90  * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
91  * @param passwd password in 'unix' charset.
92  * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
93  * @param p16 return password hashed with des, caller allocated 16 byte buffer
94  */
95  
96 /* Does both the NT and LM owfs of a user's password */
97 void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16])
98 {
99         /* Calculate the MD4 hash (NT compatible) of the password */
100         memset(nt_p16, '\0', 16);
101         E_md4hash(pwd, nt_p16);
102
103 #ifdef DEBUG_PASSWORD
104         DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
105         dump_data(120, pwd, strlen(pwd));
106         dump_data(100, (char *)nt_p16, 16);
107 #endif
108
109         E_deshash(pwd, (uchar *)p16);
110
111 #ifdef DEBUG_PASSWORD
112         DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
113         dump_data(120, pwd, strlen(pwd));
114         dump_data(100, (char *)p16, 16);
115 #endif
116 }
117
118 /* Does both the NTLMv2 owfs of a user's password */
119 BOOL ntv2_owf_gen(const uchar owf[16],
120                   const char *user_in, const char *domain_in, uchar kr_buf[16])
121 {
122         smb_ucs2_t *user;
123         smb_ucs2_t *domain;
124         
125         int user_byte_len;
126         int domain_byte_len;
127
128         HMACMD5Context ctx;
129
130         user_byte_len = push_ucs2_allocate(&user, user_in);
131         if (user_byte_len < 0) {
132                 DEBUG(0, ("push_uss2_allocate() for user returned %d (probably malloc() failure)\n", user_byte_len));
133                 return False;
134         }
135
136         domain_byte_len = push_ucs2_allocate(&domain, domain_in);
137         if (domain_byte_len < 0) {
138                 DEBUG(0, ("push_uss2_allocate() for domain returned %d (probably malloc() failure)\n", user_byte_len));
139                 return False;
140         }
141
142         strupper_w(user);
143         strupper_w(domain);
144
145         /* We don't want null termination */
146         user_byte_len = user_byte_len - 2;
147         domain_byte_len = domain_byte_len - 2;
148         
149         SMB_ASSERT(user_byte_len >= 0);
150         SMB_ASSERT(domain_byte_len >= 0);
151
152         hmac_md5_init_limK_to_64(owf, 16, &ctx);
153         hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
154         hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
155         hmac_md5_final(kr_buf, &ctx);
156
157 #ifdef DEBUG_PASSWORD
158         DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
159         dump_data(100, (const char *)user, user_byte_len);
160         dump_data(100, (const char *)domain, domain_byte_len);
161         dump_data(100, owf, 16);
162         dump_data(100, kr_buf, 16);
163 #endif
164
165         SAFE_FREE(user);
166         SAFE_FREE(domain);
167         return True;
168 }
169
170 /* Does the des encryption from the NT or LM MD4 hash. */
171 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
172 {
173         uchar p21[21];
174
175         ZERO_STRUCT(p21);
176  
177         memcpy(p21, passwd, 16);    
178         E_P24(p21, c8, p24);
179 }
180
181 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
182 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
183 {
184         uchar p21[21];
185  
186         memset(p21,'\0',21);
187         memcpy(p21, passwd, 8);    
188         memset(p21 + 8, 0xbd, 8);    
189
190         E_P24(p21, ntlmchalresp, p24);
191 #ifdef DEBUG_PASSWORD
192         DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
193         dump_data(100, (char *)p21, 21);
194         dump_data(100, (const char *)ntlmchalresp, 8);
195         dump_data(100, (char *)p24, 24);
196 #endif
197 }
198
199
200 /* Does the NT MD4 hash then des encryption. */
201  
202 void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
203 {
204         uchar p21[21];
205  
206         memset(p21,'\0',21);
207  
208         E_md4hash(passwd, p21);    
209         SMBOWFencrypt(p21, c8, p24);
210
211 #ifdef DEBUG_PASSWORD
212         DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
213         dump_data(100, (char *)p21, 16);
214         dump_data(100, (char *)c8, 8);
215         dump_data(100, (char *)p24, 24);
216 #endif
217 }
218
219 BOOL make_oem_passwd_hash(char data[516], const char *passwd, uchar old_pw_hash[16], BOOL unicode)
220 {
221         int new_pw_len = strlen(passwd) * (unicode ? 2 : 1);
222
223         if (new_pw_len > 512)
224         {
225                 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
226                 return False;
227         }
228
229         /*
230          * Now setup the data area.
231          * We need to generate a random fill
232          * for this area to make it harder to
233          * decrypt. JRA.
234          */
235         generate_random_buffer((unsigned char *)data, 516, False);
236         push_string(NULL, &data[512 - new_pw_len], passwd, new_pw_len, 
237                     STR_NOALIGN | (unicode?STR_UNICODE:STR_ASCII));
238         SIVAL(data, 512, new_pw_len);
239
240 #ifdef DEBUG_PASSWORD
241         DEBUG(100,("make_oem_passwd_hash\n"));
242         dump_data(100, data, 516);
243 #endif
244         SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
245
246         return True;
247 }
248
249 /* Does the md5 encryption from the NT hash for NTLMv2. */
250 void SMBOWFencrypt_ntv2(const uchar kr[16],
251                         const DATA_BLOB srv_chal,
252                         const DATA_BLOB cli_chal,
253                         uchar resp_buf[16])
254 {
255         HMACMD5Context ctx;
256
257         hmac_md5_init_limK_to_64(kr, 16, &ctx);
258         hmac_md5_update(srv_chal.data, srv_chal.length, &ctx);
259         hmac_md5_update(cli_chal.data, cli_chal.length, &ctx);
260         hmac_md5_final(resp_buf, &ctx);
261
262 #ifdef DEBUG_PASSWORD
263         DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
264         dump_data(100, srv_chal.data, srv_chal.length);
265         dump_data(100, cli_chal.data, cli_chal.length);
266         dump_data(100, resp_buf, 16);
267 #endif
268 }
269
270 void SMBsesskeygen_ntv2(const uchar kr[16],
271                         const uchar * nt_resp, uint8 sess_key[16])
272 {
273         HMACMD5Context ctx;
274
275         hmac_md5_init_limK_to_64(kr, 16, &ctx);
276         hmac_md5_update(nt_resp, 16, &ctx);
277         hmac_md5_final((unsigned char *)sess_key, &ctx);
278
279 #ifdef DEBUG_PASSWORD
280         DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
281         dump_data(100, sess_key, 16);
282 #endif
283 }
284
285 void SMBsesskeygen_ntv1(const uchar kr[16],
286                         const uchar * nt_resp, uint8 sess_key[16])
287 {
288         mdfour((unsigned char *)sess_key, kr, 16);
289
290 #ifdef DEBUG_PASSWORD
291         DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
292         dump_data(100, sess_key, 16);
293 #endif
294 }
295
296 /***********************************************************
297  encode a password buffer.  The caller gets to figure out 
298  what to put in it.
299 ************************************************************/
300 BOOL encode_pw_buffer(char buffer[516], char *new_pw, int new_pw_length)
301 {
302         generate_random_buffer((unsigned char *)buffer, 516, True);
303
304         memcpy(&buffer[512 - new_pw_length], new_pw, new_pw_length);
305
306         /* 
307          * The length of the new password is in the last 4 bytes of
308          * the data buffer.
309          */
310         SIVAL(buffer, 512, new_pw_length);
311
312         return True;
313 }
314
315 /***********************************************************
316  decode a password buffer
317  *new_pw_len is the length in bytes of the possibly mulitbyte
318  returned password including termination.
319 ************************************************************/
320 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
321                       int new_pwrd_size, uint32 *new_pw_len)
322 {
323         int byte_len=0;
324
325         /*
326           Warning !!! : This function is called from some rpc call.
327           The password IN the buffer is a UNICODE string.
328           The password IN new_pwrd is an ASCII string
329           If you reuse that code somewhere else check first.
330         */
331
332         /* The length of the new password is in the last 4 bytes of the data buffer. */
333
334         byte_len = IVAL(in_buffer, 512);
335
336 #ifdef DEBUG_PASSWORD
337         dump_data(100, in_buffer, 516);
338 #endif
339
340         /* Password cannot be longer than 128 characters */
341         if ( (byte_len < 0) || (byte_len > new_pwrd_size - 1)) {
342                 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
343                 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
344                 return False;
345         }
346
347         /* decode into the return buffer.  Buffer must be a pstring */
348         *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size, byte_len, STR_UNICODE);
349
350 #ifdef DEBUG_PASSWORD
351         DEBUG(100,("decode_pw_buffer: new_pwrd: "));
352         dump_data(100, (char *)new_pwrd, *new_pw_len);
353         DEBUG(100,("multibyte len:%d\n", *new_pw_len));
354         DEBUG(100,("original char len:%d\n", byte_len/2));
355 #endif
356         
357         return True;
358 }
359
360 /***********************************************************
361  SMB signing - setup the MAC key.
362 ************************************************************/
363
364 void cli_calculate_mac_key(struct cli_state *cli, const char *ntpasswd, const uchar resp[24])
365 {
366         /* Get first 16 bytes. */
367         E_md4hash(ntpasswd,&cli->sign_info.mac_key[0]);
368         memcpy(&cli->sign_info.mac_key[16],resp,24);
369         cli->sign_info.mac_key_len = 40;
370         cli->sign_info.use_smb_signing = True;
371
372         /* These calls are INCONPATIBLE with SMB signing */
373         cli->readbraw_supported = False;
374         cli->writebraw_supported = False;    
375
376         /* Reset the sequence number in case we had a previous (aborted) attempt */
377         cli->sign_info.send_seq_num = 0;
378 }
379
380 /***********************************************************
381  SMB signing - calculate a MAC to send.
382 ************************************************************/
383
384 void cli_caclulate_sign_mac(struct cli_state *cli)
385 {
386         unsigned char calc_md5_mac[16];
387         struct MD5Context md5_ctx;
388
389         if (cli->sign_info.temp_smb_signing) {
390                 memcpy(&cli->outbuf[smb_ss_field], "SignRequest", 8);
391                 cli->sign_info.temp_smb_signing = False;
392                 return;
393         }
394
395         if (!cli->sign_info.use_smb_signing) {
396                 return;
397         }
398
399         /*
400          * Firstly put the sequence number into the first 4 bytes.
401          * and zero out the next 4 bytes.
402          */
403         SIVAL(cli->outbuf, smb_ss_field, cli->sign_info.send_seq_num);
404         SIVAL(cli->outbuf, smb_ss_field + 4, 0);
405
406         /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
407         MD5Init(&md5_ctx);
408         MD5Update(&md5_ctx, cli->sign_info.mac_key, cli->sign_info.mac_key_len);
409         MD5Update(&md5_ctx, cli->outbuf + 4, smb_len(cli->outbuf));
410         MD5Final(calc_md5_mac, &md5_ctx);
411
412         memcpy(&cli->outbuf[smb_ss_field], calc_md5_mac, 8);
413 /*      cli->outbuf[smb_ss_field+2]=0; 
414         Uncomment this to test if the remote server actually verifies signitures...*/
415         cli->sign_info.send_seq_num++;
416         cli->sign_info.reply_seq_num = cli->sign_info.send_seq_num;
417         cli->sign_info.send_seq_num++;
418 }