More of SMB signing for client - not yet finished (should be harmless).
[nivanova/samba-autobuild/.git] / source3 / 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)
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 MD4 Hash of the users password in NT UNICODE.
70  * @param passwd password in 'unix' charset.
71  * @param p16 return password hashed with md4, 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. */
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 void ntv2_owf_gen(const uchar owf[16],
120                   const char *user_n, const char *domain_n, uchar kr_buf[16])
121 {
122         pstring user_u;
123         pstring dom_u;
124         HMACMD5Context ctx;
125
126         int user_l = strlen(user_n);
127         int domain_l = strlen(domain_n);
128
129         push_ucs2(NULL, user_u, user_n, (user_l+1)*2, STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
130         push_ucs2(NULL, dom_u, domain_n, (domain_l+1)*2, STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
131
132         hmac_md5_init_limK_to_64(owf, 16, &ctx);
133         hmac_md5_update((const unsigned char *)user_u, user_l * 2, &ctx);
134         hmac_md5_update((const unsigned char *)dom_u, domain_l * 2, &ctx);
135         hmac_md5_final(kr_buf, &ctx);
136
137 #ifdef DEBUG_PASSWORD
138         DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
139         dump_data(100, user_u, user_l * 2);
140         dump_data(100, dom_u, domain_l * 2);
141         dump_data(100, owf, 16);
142         dump_data(100, kr_buf, 16);
143 #endif
144 }
145
146 /* Does the des encryption from the NT or LM MD4 hash. */
147 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
148 {
149         uchar p21[21];
150  
151         memset(p21,'\0',21);
152  
153         memcpy(p21, passwd, 16);    
154         E_P24(p21, c8, p24);
155 }
156
157 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
158 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
159 {
160         uchar p21[21];
161  
162         memset(p21,'\0',21);
163         memcpy(p21, passwd, 8);    
164         memset(p21 + 8, 0xbd, 8);    
165
166         E_P24(p21, ntlmchalresp, p24);
167 #ifdef DEBUG_PASSWORD
168         DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
169         dump_data(100, (char *)p21, 21);
170         dump_data(100, (const char *)ntlmchalresp, 8);
171         dump_data(100, (char *)p24, 24);
172 #endif
173 }
174
175
176 /* Does the NT MD4 hash then des encryption. */
177  
178 void SMBNTencrypt(const uchar *passwd, uchar *c8, uchar *p24)
179 {
180         uchar p21[21];
181  
182         memset(p21,'\0',21);
183  
184         E_md4hash(passwd, p21);    
185         SMBOWFencrypt(p21, c8, p24);
186
187 #ifdef DEBUG_PASSWORD
188         DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
189         dump_data(100, (char *)p21, 16);
190         dump_data(100, (char *)c8, 8);
191         dump_data(100, (char *)p24, 24);
192 #endif
193 }
194
195 BOOL make_oem_passwd_hash(char data[516], const char *passwd, uchar old_pw_hash[16], BOOL unicode)
196 {
197         int new_pw_len = strlen(passwd) * (unicode ? 2 : 1);
198
199         if (new_pw_len > 512)
200         {
201                 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
202                 return False;
203         }
204
205         /*
206          * Now setup the data area.
207          * We need to generate a random fill
208          * for this area to make it harder to
209          * decrypt. JRA.
210          */
211         generate_random_buffer((unsigned char *)data, 516, False);
212         push_string(NULL, &data[512 - new_pw_len], passwd, new_pw_len, 
213                     STR_NOALIGN | (unicode?STR_UNICODE:STR_ASCII));
214         SIVAL(data, 512, new_pw_len);
215
216 #ifdef DEBUG_PASSWORD
217         DEBUG(100,("make_oem_passwd_hash\n"));
218         dump_data(100, data, 516);
219 #endif
220         SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
221
222         return True;
223 }
224
225 /* Does the md5 encryption from the NT hash for NTLMv2. */
226 void SMBOWFencrypt_ntv2(const uchar kr[16],
227                         const DATA_BLOB srv_chal,
228                         const DATA_BLOB cli_chal,
229                         char resp_buf[16])
230 {
231         HMACMD5Context ctx;
232
233         hmac_md5_init_limK_to_64(kr, 16, &ctx);
234         hmac_md5_update(srv_chal.data, srv_chal.length, &ctx);
235         hmac_md5_update(cli_chal.data, cli_chal.length, &ctx);
236         hmac_md5_final((unsigned char *)resp_buf, &ctx);
237
238 #ifdef DEBUG_PASSWORD
239         DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
240         dump_data(100, srv_chal.data, srv_chal.length);
241         dump_data(100, cli_chal.data, cli_chal.length);
242         dump_data(100, resp_buf, 16);
243 #endif
244 }
245
246 void SMBsesskeygen_ntv2(const uchar kr[16],
247                         const uchar * nt_resp, uint8 sess_key[16])
248 {
249         HMACMD5Context ctx;
250
251         hmac_md5_init_limK_to_64(kr, 16, &ctx);
252         hmac_md5_update(nt_resp, 16, &ctx);
253         hmac_md5_final((unsigned char *)sess_key, &ctx);
254
255 #ifdef DEBUG_PASSWORD
256         DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
257         dump_data(100, sess_key, 16);
258 #endif
259 }
260
261 void SMBsesskeygen_ntv1(const uchar kr[16],
262                         const uchar * nt_resp, uint8 sess_key[16])
263 {
264         mdfour((unsigned char *)sess_key, kr, 16);
265
266 #ifdef DEBUG_PASSWORD
267         DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
268         dump_data(100, sess_key, 16);
269 #endif
270 }
271
272 /***********************************************************
273  encode a password buffer.  The caller gets to figure out 
274  what to put in it.
275 ************************************************************/
276 BOOL encode_pw_buffer(char buffer[516], char *new_pw, int new_pw_length)
277 {
278         generate_random_buffer((unsigned char *)buffer, 516, True);
279
280         memcpy(&buffer[512 - new_pw_length], new_pw, new_pw_length);
281
282         /* 
283          * The length of the new password is in the last 4 bytes of
284          * the data buffer.
285          */
286         SIVAL(buffer, 512, new_pw_length);
287
288         return True;
289 }
290
291 /***********************************************************
292  decode a password buffer
293  *new_pw_len is the length in bytes of the possibly mulitbyte
294  returned password including termination.
295 ************************************************************/
296 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
297                       int new_pwrd_size, uint32 *new_pw_len)
298 {
299         int byte_len=0;
300
301         /*
302           Warning !!! : This function is called from some rpc call.
303           The password IN the buffer is a UNICODE string.
304           The password IN new_pwrd is an ASCII string
305           If you reuse that code somewhere else check first.
306         */
307
308         /* The length of the new password is in the last 4 bytes of the data buffer. */
309
310         byte_len = IVAL(in_buffer, 512);
311
312 #ifdef DEBUG_PASSWORD
313         dump_data(100, in_buffer, 516);
314 #endif
315
316         /* Password cannot be longer than 128 characters */
317         if ( (byte_len < 0) || (byte_len > new_pwrd_size - 1)) {
318                 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
319                 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
320                 return False;
321         }
322
323         /* decode into the return buffer.  Buffer must be a pstring */
324         *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size, byte_len, STR_UNICODE);
325
326 #ifdef DEBUG_PASSWORD
327         DEBUG(100,("decode_pw_buffer: new_pwrd: "));
328         dump_data(100, (char *)new_pwrd, *new_pw_len);
329         DEBUG(100,("multibyte len:%d\n", *new_pw_len));
330         DEBUG(100,("original char len:%d\n", byte_len/2));
331 #endif
332         
333         return True;
334 }
335
336 /***********************************************************
337  SMB signing - calculate a MAC to send.
338 ************************************************************/
339
340 void cli_caclulate_sign_mac(struct cli_state *cli)
341 {
342         unsigned char calc_md5_mac[16];
343         struct MD5Context md5_ctx;
344
345         /*
346          * Firstly put the sequence number into the first 4 bytes.
347          * and zero out the next 4 bytes.
348          */
349         SIVAL(cli->outbuf, smb_ss_field, cli->sign_info.send_seq_num);
350         SIVAL(cli->outbuf, smb_ss_field + 4, 0);
351
352         /* Calculate the 16 byte MAC and place first 8 bytes into the field. */
353         MD5Init(&md5_ctx);
354         MD5Update(&md5_ctx, cli->sign_info.mac_key, cli->sign_info.mac_key_len);
355         MD5Update(&md5_ctx, cli->outbuf + 4, smb_len(cli->outbuf));
356         MD5Final(calc_md5_mac, &md5_ctx);
357
358         memcpy(&cli->outbuf[smb_ss_field], calc_md5_mac, 8);
359         cli->sign_info.send_seq_num++;
360         cli->sign_info.reply_seq_num = cli->sign_info.send_seq_num;
361         cli->sign_info.send_seq_num++;
362 }