bcb40acda19c4a6665259b8a2406c89178fa623f
[kai/samba.git] / libcli / auth / 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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/time.h"
26 #include "auth/ntlmssp/ntlmssp.h"
27 #include "auth/ntlmssp/msrpc_parse.h"
28 #include "../lib/crypto/crypto.h"
29 #include "libcli/auth/libcli_auth.h"
30
31 void SMBencrypt_hash(const uint8_t lm_hash[16], const uint8_t *c8, uint8_t p24[24])
32 {
33         uint8_t p21[21];
34
35         memset(p21,'\0',21);
36         memcpy(p21, lm_hash, 16);
37
38         SMBOWFencrypt(p21, c8, p24);
39
40 #ifdef DEBUG_PASSWORD
41         DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
42         dump_data(100, p21, 16);
43         dump_data(100, c8, 8);
44         dump_data(100, p24, 24);
45 #endif
46 }
47
48 /*
49    This implements the X/Open SMB password encryption
50    It takes a password ('unix' string), a 8 byte "crypt key" 
51    and puts 24 bytes of encrypted password into p24 
52
53    Returns False if password must have been truncated to create LM hash
54 */
55
56 bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24])
57 {
58         bool ret;
59         uint8_t lm_hash[16];
60
61         ret = E_deshash(passwd, lm_hash); 
62         SMBencrypt_hash(lm_hash, c8, p24);
63         return ret;
64 }
65
66 /**
67  * Creates the MD4 Hash of the users password in NT UNICODE.
68  * @param passwd password in 'unix' charset.
69  * @param p16 return password hashed with md4, caller allocated 16 byte buffer
70  */
71  
72 bool E_md4hash(const char *passwd, uint8_t p16[16])
73 {
74         size_t len;
75         smb_ucs2_t *wpwd;
76         bool ret;
77
78         ret = push_ucs2_talloc(NULL, &wpwd, passwd, &len);
79         if (!ret || len < 2) {
80                 /* We don't want to return fixed data, as most callers
81                  * don't check */
82                 mdfour(p16, (const uint8_t *)passwd, strlen(passwd));
83                 return false;
84         }
85         
86         len -= 2;
87         mdfour(p16, (const uint8_t *)wpwd, len);
88
89         talloc_free(wpwd);
90         return true;
91 }
92
93 /**
94  * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash.
95  * @param 16 byte salt.
96  * @param 16 byte NT hash.
97  * @param 16 byte return hashed with md5, caller allocated 16 byte buffer
98  */
99
100 void E_md5hash(const uint8_t salt[16], const uint8_t nthash[16], uint8_t hash_out[16])
101 {
102         struct MD5Context tctx;
103         uint8_t array[32];
104         
105         memset(hash_out, '\0', 16);
106         memcpy(array, salt, 16);
107         memcpy(&array[16], nthash, 16);
108         MD5Init(&tctx);
109         MD5Update(&tctx, array, 32);
110         MD5Final(hash_out, &tctx);
111 }
112
113 /**
114  * Creates the DES forward-only Hash of the users password in DOS ASCII charset
115  * @param passwd password in 'unix' charset.
116  * @param p16 return password hashed with DES, caller allocated 16 byte buffer
117  * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true
118  * @note p16 is filled in regardless
119  */
120  
121 bool E_deshash(const char *passwd, uint8_t p16[16])
122 {
123         bool ret = true;
124         char dospwd[256];
125         ZERO_STRUCT(dospwd);
126
127         /* Password must be converted to DOS charset - null terminated, uppercase. */
128         push_string(dospwd, passwd, sizeof(dospwd), STR_ASCII|STR_UPPER|STR_TERMINATE);
129
130         /* Only the first 14 chars are considered, password need not be null terminated. */
131         E_P16((const uint8_t *)dospwd, p16);
132
133         if (strlen(dospwd) > 14) {
134                 ret = false;
135         }
136
137         ZERO_STRUCT(dospwd);    
138
139         return ret;
140 }
141
142 /**
143  * Creates the MD4 and DES (LM) Hash of the users password.  
144  * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
145  * @param passwd password in 'unix' charset.
146  * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
147  * @param p16 return password hashed with des, caller allocated 16 byte buffer
148  */
149  
150 /* Does both the NT and LM owfs of a user's password */
151 void nt_lm_owf_gen(const char *pwd, uint8_t nt_p16[16], uint8_t p16[16])
152 {
153         /* Calculate the MD4 hash (NT compatible) of the password */
154         memset(nt_p16, '\0', 16);
155         E_md4hash(pwd, nt_p16);
156
157 #ifdef DEBUG_PASSWORD
158         DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
159         dump_data(120, (uint8_t *)pwd, strlen(pwd));
160         dump_data(100, nt_p16, 16);
161 #endif
162
163         E_deshash(pwd, (uint8_t *)p16);
164
165 #ifdef DEBUG_PASSWORD
166         DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
167         dump_data(120, (uint8_t *)pwd, strlen(pwd));
168         dump_data(100, p16, 16);
169 #endif
170 }
171
172 /* Does both the NTLMv2 owfs of a user's password */
173 bool ntv2_owf_gen(const uint8_t owf[16],
174                   const char *user_in, const char *domain_in,
175                   bool upper_case_domain, /* Transform the domain into UPPER case */
176                   uint8_t kr_buf[16])
177 {
178         smb_ucs2_t *user;
179         smb_ucs2_t *domain;     
180         size_t user_byte_len;
181         size_t domain_byte_len;
182         bool ret;
183
184         HMACMD5Context ctx;
185         TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in); 
186
187         if (!mem_ctx) {
188                 return false;
189         }
190
191         if (!user_in) {
192                 user_in = "";
193         }
194
195         if (!domain_in) {
196                 domain_in = "";
197         }
198
199         user_in = strupper_talloc(mem_ctx, user_in);
200         if (user_in == NULL) {
201                 talloc_free(mem_ctx);
202                 return false;
203         }
204
205         if (upper_case_domain) {
206                 domain_in = strupper_talloc(mem_ctx, domain_in);
207                 if (domain_in == NULL) {
208                         talloc_free(mem_ctx);
209                         return false;
210                 }
211         }
212
213         ret = push_ucs2_talloc(mem_ctx, &user, user_in, &user_byte_len );
214         if (!ret) {
215                 DEBUG(0, ("push_uss2_talloc() for user failed)\n"));
216                 talloc_free(mem_ctx);
217                 return false;
218         }
219
220         ret = push_ucs2_talloc(mem_ctx, &domain, domain_in, &domain_byte_len);
221         if (!ret) {
222                 DEBUG(0, ("push_ucs2_talloc() for domain failed\n"));
223                 talloc_free(mem_ctx);
224                 return false;
225         }
226
227         SMB_ASSERT(user_byte_len >= 2);
228         SMB_ASSERT(domain_byte_len >= 2);
229
230         /* We don't want null termination */
231         user_byte_len = user_byte_len - 2;
232         domain_byte_len = domain_byte_len - 2;
233         
234         hmac_md5_init_limK_to_64(owf, 16, &ctx);
235         hmac_md5_update((const void *)user, user_byte_len, &ctx);
236         hmac_md5_update((const void *)domain, domain_byte_len, &ctx);
237         hmac_md5_final(kr_buf, &ctx);
238
239 #ifdef DEBUG_PASSWORD
240         DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
241         dump_data(100, (const void *)user, user_byte_len);
242         dump_data(100, (const void *)domain, domain_byte_len);
243         dump_data(100, owf, 16);
244         dump_data(100, kr_buf, 16);
245 #endif
246
247         talloc_free(mem_ctx);
248         return true;
249 }
250
251 /* Does the des encryption from the NT or LM MD4 hash. */
252 void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24])
253 {
254         uint8_t p21[21];
255
256         ZERO_STRUCT(p21);
257  
258         memcpy(p21, passwd, 16);    
259         E_P24(p21, c8, p24);
260 }
261
262 /* Does the des encryption. */
263  
264 void SMBNTencrypt_hash(const uint8_t nt_hash[16], uint8_t *c8, uint8_t *p24)
265 {
266         uint8_t p21[21];
267  
268         memset(p21,'\0',21);
269         memcpy(p21, nt_hash, 16);
270         SMBOWFencrypt(p21, c8, p24);
271
272 #ifdef DEBUG_PASSWORD
273         DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
274         dump_data(100, p21, 16);
275         dump_data(100, c8, 8);
276         dump_data(100, p24, 24);
277 #endif
278 }
279
280 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
281
282 void SMBNTencrypt(const char *passwd, uint8_t *c8, uint8_t *p24)
283 {
284         uint8_t nt_hash[16];
285         E_md4hash(passwd, nt_hash);    
286         SMBNTencrypt_hash(nt_hash, c8, p24);
287 }
288
289
290 /* Does the md5 encryption from the Key Response for NTLMv2. */
291 void SMBOWFencrypt_ntv2(const uint8_t kr[16],
292                         const DATA_BLOB *srv_chal,
293                         const DATA_BLOB *smbcli_chal,
294                         uint8_t resp_buf[16])
295 {
296         HMACMD5Context ctx;
297
298         hmac_md5_init_limK_to_64(kr, 16, &ctx);
299         hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
300         hmac_md5_update(smbcli_chal->data, smbcli_chal->length, &ctx);
301         hmac_md5_final(resp_buf, &ctx);
302
303 #ifdef DEBUG_PASSWORD
304         DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));
305         dump_data(100, srv_chal->data, srv_chal->length);
306         dump_data(100, smbcli_chal->data, smbcli_chal->length);
307         dump_data(100, resp_buf, 16);
308 #endif
309 }
310
311 void SMBsesskeygen_ntv2(const uint8_t kr[16],
312                         const uint8_t * nt_resp, uint8_t sess_key[16])
313 {
314         /* a very nice, 128 bit, variable session key */
315         
316         HMACMD5Context ctx;
317
318         hmac_md5_init_limK_to_64(kr, 16, &ctx);
319         hmac_md5_update(nt_resp, 16, &ctx);
320         hmac_md5_final((uint8_t *)sess_key, &ctx);
321
322 #ifdef DEBUG_PASSWORD
323         DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
324         dump_data(100, sess_key, 16);
325 #endif
326 }
327
328 void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16])
329 {
330         /* yes, this session key does not change - yes, this 
331            is a problem - but it is 128 bits */
332         
333         mdfour((uint8_t *)sess_key, kr, 16);
334
335 #ifdef DEBUG_PASSWORD
336         DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
337         dump_data(100, sess_key, 16);
338 #endif
339 }
340
341 void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
342                                const uint8_t lm_resp[24], /* only uses 8 */ 
343                                uint8_t sess_key[16])
344 {
345         /* Calculate the LM session key (effective length 40 bits,
346            but changes with each session) */
347         uint8_t p24[24];
348         uint8_t partial_lm_hash[14];
349  
350         memcpy(partial_lm_hash, lm_hash, 8);    
351         memset(partial_lm_hash + 8, 0xbd, 6);
352
353         des_crypt56(p24,   lm_resp, partial_lm_hash,     1);
354         des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
355
356         memcpy(sess_key, p24, 16);
357
358 #ifdef DEBUG_PASSWORD
359         DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
360         dump_data(100, sess_key, 16);
361 #endif
362 }
363
364 DATA_BLOB NTLMv2_generate_names_blob(TALLOC_CTX *mem_ctx, 
365                                      const char *hostname, 
366                                      const char *domain)
367 {
368         DATA_BLOB names_blob = data_blob_talloc(mem_ctx, NULL, 0);
369         
370         msrpc_gen(mem_ctx, &names_blob, 
371                   "aaa", 
372                   NTLMSSP_NAME_TYPE_DOMAIN, domain,
373                   NTLMSSP_NAME_TYPE_SERVER, hostname,
374                   0, "");
375         return names_blob;
376 }
377
378 static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx, const DATA_BLOB *names_blob) 
379 {
380         uint8_t client_chal[8];
381         DATA_BLOB response = data_blob(NULL, 0);
382         uint8_t long_date[8];
383         NTTIME nttime;
384
385         unix_to_nt_time(&nttime, time(NULL));
386
387         generate_random_buffer(client_chal, sizeof(client_chal));
388
389         push_nttime(long_date, 0, nttime);
390
391         /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
392
393         msrpc_gen(mem_ctx, &response, "ddbbdb", 
394                   0x00000101,     /* Header  */
395                   0,              /* 'Reserved'  */
396                   long_date, 8,   /* Timestamp */
397                   client_chal, 8, /* client challenge */
398                   0,              /* Unknown */
399                   names_blob->data, names_blob->length);        /* End of name list */
400
401         return response;
402 }
403
404 static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx, 
405                                           const uint8_t ntlm_v2_hash[16],
406                                           const DATA_BLOB *server_chal,
407                                           const DATA_BLOB *names_blob)
408 {
409         uint8_t ntlmv2_response[16];
410         DATA_BLOB ntlmv2_client_data;
411         DATA_BLOB final_response;
412         
413         TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0, 
414                                            "NTLMv2_generate_response internal context");
415
416         if (!mem_ctx) {
417                 return data_blob(NULL, 0);
418         }
419         
420         /* NTLMv2 */
421         /* generate some data to pass into the response function - including
422            the hostname and domain name of the server */
423         ntlmv2_client_data = NTLMv2_generate_client_data(mem_ctx, names_blob);
424
425         /* Given that data, and the challenge from the server, generate a response */
426         SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
427         
428         final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
429
430         memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
431
432         memcpy(final_response.data+sizeof(ntlmv2_response), 
433                ntlmv2_client_data.data, ntlmv2_client_data.length);
434
435         talloc_free(mem_ctx);
436
437         return final_response;
438 }
439
440 static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx, 
441                                         const uint8_t ntlm_v2_hash[16],
442                                         const DATA_BLOB *server_chal)
443 {
444         uint8_t lmv2_response[16];
445         DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
446         DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
447         
448         /* LMv2 */
449         /* client-supplied random data */
450         generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length); 
451
452         /* Given that data, and the challenge from the server, generate a response */
453         SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
454         memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
455
456         /* after the first 16 bytes is the random data we generated above, 
457            so the server can verify us with it */
458         memcpy(final_response.data+sizeof(lmv2_response), 
459                lmv2_client_data.data, lmv2_client_data.length);
460
461         data_blob_free(&lmv2_client_data);
462
463         return final_response;
464 }
465
466 bool SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx, 
467                            const char *user, const char *domain, const uint8_t nt_hash[16],
468                            const DATA_BLOB *server_chal, 
469                            const DATA_BLOB *names_blob,
470                            DATA_BLOB *lm_response, DATA_BLOB *nt_response, 
471                            DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key) 
472 {
473         uint8_t ntlm_v2_hash[16];
474
475         /* We don't use the NT# directly.  Instead we use it mashed up with
476            the username and domain.
477            This prevents username swapping during the auth exchange
478         */
479         if (!ntv2_owf_gen(nt_hash, user, domain, true, ntlm_v2_hash)) {
480                 return false;
481         }
482         
483         if (nt_response) {
484                 *nt_response = NTLMv2_generate_response(mem_ctx, 
485                                                         ntlm_v2_hash, server_chal,
486                                                         names_blob); 
487                 if (user_session_key) {
488                         *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
489                         
490                         /* The NTLMv2 calculations also provide a session key, for signing etc later */
491                         /* use only the first 16 bytes of nt_response for session key */
492                         SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
493                 }
494         }
495         
496         /* LMv2 */
497         
498         if (lm_response) {
499                 *lm_response = LMv2_generate_response(mem_ctx, 
500                                                       ntlm_v2_hash, server_chal);
501                 if (lm_session_key) {
502                         *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
503                         
504                         /* The NTLMv2 calculations also provide a session key, for signing etc later */
505                         /* use only the first 16 bytes of lm_response for session key */
506                         SMBsesskeygen_ntv2(ntlm_v2_hash, lm_response->data, lm_session_key->data);
507                 }
508         }
509         
510         return true;
511 }
512
513 bool SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx, 
514                       const char *user, const char *domain, 
515                       const char *password, 
516                       const DATA_BLOB *server_chal, 
517                       const DATA_BLOB *names_blob,
518                       DATA_BLOB *lm_response, DATA_BLOB *nt_response, 
519                       DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key) 
520 {
521         uint8_t nt_hash[16];
522         E_md4hash(password, nt_hash);
523
524         return SMBNTLMv2encrypt_hash(mem_ctx, 
525                                      user, domain, nt_hash, server_chal, names_blob,
526                                      lm_response, nt_response, lm_session_key, user_session_key);
527 }
528
529 /***********************************************************
530  encode a password buffer with a unicode password.  The buffer
531  is filled with random data to make it harder to attack.
532 ************************************************************/
533 bool encode_pw_buffer(uint8_t buffer[516], const char *password, int string_flags)
534 {
535         uint8_t new_pw[512];
536         size_t new_pw_len;
537
538         /* the incoming buffer can be any alignment. */
539         string_flags |= STR_NOALIGN;
540
541         new_pw_len = push_string(new_pw, new_pw,
542                                  password, 
543                                  sizeof(new_pw), string_flags);
544         
545         memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
546
547         generate_random_buffer(buffer, 512 - new_pw_len);
548
549         /* 
550          * The length of the new password is in the last 4 bytes of
551          * the data buffer.
552          */
553         SIVAL(buffer, 512, new_pw_len);
554         ZERO_STRUCT(new_pw);
555         return true;
556 }
557
558
559 /***********************************************************
560  decode a password buffer
561  *new_pw_len is the length in bytes of the possibly mulitbyte
562  returned password including termination.
563 ************************************************************/
564
565 bool decode_pw_buffer(TALLOC_CTX *ctx,
566                       uint8_t in_buffer[516],
567                       char **pp_new_pwrd,
568                       size_t *new_pw_len,
569                       charset_t string_charset)
570 {
571         int byte_len=0;
572
573         *pp_new_pwrd = NULL;
574         *new_pw_len = 0;
575
576         /*
577           Warning !!! : This function is called from some rpc call.
578           The password IN the buffer may be a UNICODE string.
579           The password IN new_pwrd is an ASCII string
580           If you reuse that code somewhere else check first.
581         */
582
583         /* The length of the new password is in the last 4 bytes of the data buffer. */
584
585         byte_len = IVAL(in_buffer, 512);
586
587 #ifdef DEBUG_PASSWORD
588         dump_data(100, in_buffer, 516);
589 #endif
590
591         /* Password cannot be longer than the size of the password buffer */
592         if ( (byte_len < 0) || (byte_len > 512)) {
593                 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
594                 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
595                 return false;
596         }
597
598         /* decode into the return buffer. */
599         if (!convert_string_talloc(ctx, string_charset, CH_UNIX, 
600                                    &in_buffer[512 - byte_len],
601                                    byte_len,
602                                    (void *)pp_new_pwrd,
603                                    new_pw_len,
604                                    false)) {
605                 DEBUG(0, ("decode_pw_buffer: failed to convert incoming password\n"));
606                 return false;
607         }
608
609 #ifdef DEBUG_PASSWORD
610         DEBUG(100,("decode_pw_buffer: new_pwrd: "));
611         dump_data(100, (uint8_t *)*pp_new_pwrd, *new_pw_len);
612         DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len));
613         DEBUG(100,("original char len:%d\n", byte_len/2));
614 #endif
615
616         return true;
617 }
618
619 /***********************************************************
620  Decode an arc4 encrypted password change buffer.
621 ************************************************************/
622
623 void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
624 {
625         struct MD5Context tctx;
626         unsigned char key_out[16];
627
628         /* Confounder is last 16 bytes. */
629
630         MD5Init(&tctx);
631         MD5Update(&tctx, &pw_buf[516], 16);
632         MD5Update(&tctx, psession_key->data, psession_key->length);
633         MD5Final(key_out, &tctx);
634         /* arc4 with key_out. */
635         arcfour_crypt(pw_buf, key_out, 516);
636 }
637
638 /***********************************************************
639  encode a password buffer with an already unicode password.  The
640  rest of the buffer is filled with random data to make it harder to attack.
641 ************************************************************/
642 bool set_pw_in_buffer(uint8_t buffer[516], DATA_BLOB *password)
643 {
644         if (password->length > 512) {
645                 return false;
646         }
647
648         memcpy(&buffer[512 - password->length], password->data, password->length);
649
650         generate_random_buffer(buffer, 512 - password->length);
651
652         /* 
653          * The length of the new password is in the last 4 bytes of
654          * the data buffer.
655          */
656         SIVAL(buffer, 512, password->length);
657         return true;
658 }
659
660 /***********************************************************
661  decode a password buffer
662  *new_pw_size is the length in bytes of the extracted unicode password
663 ************************************************************/
664 bool extract_pw_from_buffer(TALLOC_CTX *mem_ctx, 
665                             uint8_t in_buffer[516], DATA_BLOB *new_pass)
666 {
667         int byte_len=0;
668
669         /* The length of the new password is in the last 4 bytes of the data buffer. */
670
671         byte_len = IVAL(in_buffer, 512);
672
673 #ifdef DEBUG_PASSWORD
674         dump_data(100, in_buffer, 516);
675 #endif
676
677         /* Password cannot be longer than the size of the password buffer */
678         if ( (byte_len < 0) || (byte_len > 512)) {
679                 return false;
680         }
681
682         *new_pass = data_blob_talloc(mem_ctx, &in_buffer[512 - byte_len], byte_len);
683
684         if (!new_pass->data) {
685                 return false;
686         }
687
688         return true;
689 }
690
691
692 /* encode a wkssvc_PasswordBuffer:
693  *
694  * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
695  * 16byte), confounder in front of the 516 byte buffer (instead of after that
696  * buffer), calling MD5Update() first with session_key and then with confounder
697  * (vice versa in samr) - Guenther */
698
699 void encode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
700                                         const char *pwd,
701                                         DATA_BLOB *session_key,
702                                         struct wkssvc_PasswordBuffer **pwd_buf)
703 {
704         uint8_t buffer[516];
705         struct MD5Context ctx;
706         struct wkssvc_PasswordBuffer *my_pwd_buf = NULL;
707         DATA_BLOB confounded_session_key;
708         int confounder_len = 8;
709         uint8_t confounder[8];
710
711         my_pwd_buf = talloc_zero(mem_ctx, struct wkssvc_PasswordBuffer);
712         if (!my_pwd_buf) {
713                 return;
714         }
715
716         confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
717
718         encode_pw_buffer(buffer, pwd, STR_UNICODE);
719
720         generate_random_buffer((uint8_t *)confounder, confounder_len);
721
722         MD5Init(&ctx);
723         MD5Update(&ctx, session_key->data, session_key->length);
724         MD5Update(&ctx, confounder, confounder_len);
725         MD5Final(confounded_session_key.data, &ctx);
726
727         arcfour_crypt_blob(buffer, 516, &confounded_session_key);
728
729         memcpy(&my_pwd_buf->data[0], confounder, confounder_len);
730         memcpy(&my_pwd_buf->data[8], buffer, 516);
731
732         data_blob_free(&confounded_session_key);
733
734         *pwd_buf = my_pwd_buf;
735 }
736
737 WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
738                                           struct wkssvc_PasswordBuffer *pwd_buf,
739                                           DATA_BLOB *session_key,
740                                           char **pwd)
741 {
742         uint8_t buffer[516];
743         struct MD5Context ctx;
744         size_t pwd_len;
745
746         DATA_BLOB confounded_session_key;
747
748         int confounder_len = 8;
749         uint8_t confounder[8];
750
751         *pwd = NULL;
752
753         if (!pwd_buf) {
754                 return WERR_BAD_PASSWORD;
755         }
756
757         if (session_key->length != 16) {
758                 DEBUG(10,("invalid session key\n"));
759                 return WERR_BAD_PASSWORD;
760         }
761
762         confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
763
764         memcpy(&confounder, &pwd_buf->data[0], confounder_len);
765         memcpy(&buffer, &pwd_buf->data[8], 516);
766
767         MD5Init(&ctx);
768         MD5Update(&ctx, session_key->data, session_key->length);
769         MD5Update(&ctx, confounder, confounder_len);
770         MD5Final(confounded_session_key.data, &ctx);
771
772         arcfour_crypt_blob(buffer, 516, &confounded_session_key);
773
774         if (!decode_pw_buffer(mem_ctx, buffer, pwd, &pwd_len, CH_UTF16)) {
775                 data_blob_free(&confounded_session_key);
776                 return WERR_BAD_PASSWORD;
777         }
778
779         data_blob_free(&confounded_session_key);
780
781         return WERR_OK;
782 }
783