Rework Samba4 to use the new common libcli/auth code
[Samba/gbeck.git] / libcli / auth / smbencrypt.c
blob7659446b75dfd063e438da9a10b16f59e7fac7c3
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
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.
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.
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/>.
24 #include "includes.h"
25 #include "system/time.h"
26 #include "../libcli/auth/msrpc_parse.h"
27 #include "../lib/crypto/crypto.h"
28 #include "../libcli/auth/libcli_auth.h"
30 void SMBencrypt_hash(const uint8_t lm_hash[16], const uint8_t *c8, uint8_t p24[24])
32 uint8_t p21[21];
34 memset(p21,'\0',21);
35 memcpy(p21, lm_hash, 16);
37 SMBOWFencrypt(p21, c8, p24);
39 #ifdef DEBUG_PASSWORD
40 DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
41 dump_data(100, p21, 16);
42 dump_data(100, c8, 8);
43 dump_data(100, p24, 24);
44 #endif
48 This implements the X/Open SMB password encryption
49 It takes a password ('unix' string), a 8 byte "crypt key"
50 and puts 24 bytes of encrypted password into p24
52 Returns False if password must have been truncated to create LM hash
55 bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24])
57 bool ret;
58 uint8_t lm_hash[16];
60 ret = E_deshash(passwd, lm_hash);
61 SMBencrypt_hash(lm_hash, c8, p24);
62 return ret;
65 /**
66 * Creates the MD4 Hash of the users password in NT UNICODE.
67 * @param passwd password in 'unix' charset.
68 * @param p16 return password hashed with md4, caller allocated 16 byte buffer
71 bool E_md4hash(const char *passwd, uint8_t p16[16])
73 size_t len;
74 smb_ucs2_t *wpwd;
75 bool ret;
77 ret = push_ucs2_talloc(NULL, &wpwd, passwd, &len);
78 if (!ret || len < 2) {
79 /* We don't want to return fixed data, as most callers
80 * don't check */
81 mdfour(p16, (const uint8_t *)passwd, strlen(passwd));
82 return false;
85 len -= 2;
86 mdfour(p16, (const uint8_t *)wpwd, len);
88 talloc_free(wpwd);
89 return true;
92 /**
93 * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash.
94 * @param 16 byte salt.
95 * @param 16 byte NT hash.
96 * @param 16 byte return hashed with md5, caller allocated 16 byte buffer
99 void E_md5hash(const uint8_t salt[16], const uint8_t nthash[16], uint8_t hash_out[16])
101 struct MD5Context tctx;
102 uint8_t array[32];
104 memset(hash_out, '\0', 16);
105 memcpy(array, salt, 16);
106 memcpy(&array[16], nthash, 16);
107 MD5Init(&tctx);
108 MD5Update(&tctx, array, 32);
109 MD5Final(hash_out, &tctx);
113 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
114 * @param passwd password in 'unix' charset.
115 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
116 * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true
117 * @note p16 is filled in regardless
120 bool E_deshash(const char *passwd, uint8_t p16[16])
122 bool ret = true;
123 char dospwd[256];
124 ZERO_STRUCT(dospwd);
126 /* Password must be converted to DOS charset - null terminated, uppercase. */
127 push_string(dospwd, passwd, sizeof(dospwd), STR_ASCII|STR_UPPER|STR_TERMINATE);
129 /* Only the first 14 chars are considered, password need not be null terminated. */
130 E_P16((const uint8_t *)dospwd, p16);
132 if (strlen(dospwd) > 14) {
133 ret = false;
136 ZERO_STRUCT(dospwd);
138 return ret;
142 * Creates the MD4 and DES (LM) Hash of the users password.
143 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
144 * @param passwd password in 'unix' charset.
145 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
146 * @param p16 return password hashed with des, caller allocated 16 byte buffer
149 /* Does both the NT and LM owfs of a user's password */
150 void nt_lm_owf_gen(const char *pwd, uint8_t nt_p16[16], uint8_t p16[16])
152 /* Calculate the MD4 hash (NT compatible) of the password */
153 memset(nt_p16, '\0', 16);
154 E_md4hash(pwd, nt_p16);
156 #ifdef DEBUG_PASSWORD
157 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
158 dump_data(120, (uint8_t *)pwd, strlen(pwd));
159 dump_data(100, nt_p16, 16);
160 #endif
162 E_deshash(pwd, (uint8_t *)p16);
164 #ifdef DEBUG_PASSWORD
165 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
166 dump_data(120, (uint8_t *)pwd, strlen(pwd));
167 dump_data(100, p16, 16);
168 #endif
171 /* Does both the NTLMv2 owfs of a user's password */
172 bool ntv2_owf_gen(const uint8_t owf[16],
173 const char *user_in, const char *domain_in,
174 bool upper_case_domain, /* Transform the domain into UPPER case */
175 uint8_t kr_buf[16])
177 smb_ucs2_t *user;
178 smb_ucs2_t *domain;
179 size_t user_byte_len;
180 size_t domain_byte_len;
181 bool ret;
183 HMACMD5Context ctx;
184 TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in);
186 if (!mem_ctx) {
187 return false;
190 if (!user_in) {
191 user_in = "";
194 if (!domain_in) {
195 domain_in = "";
198 user_in = strupper_talloc(mem_ctx, user_in);
199 if (user_in == NULL) {
200 talloc_free(mem_ctx);
201 return false;
204 if (upper_case_domain) {
205 domain_in = strupper_talloc(mem_ctx, domain_in);
206 if (domain_in == NULL) {
207 talloc_free(mem_ctx);
208 return false;
212 ret = push_ucs2_talloc(mem_ctx, &user, user_in, &user_byte_len );
213 if (!ret) {
214 DEBUG(0, ("push_uss2_talloc() for user failed)\n"));
215 talloc_free(mem_ctx);
216 return false;
219 ret = push_ucs2_talloc(mem_ctx, &domain, domain_in, &domain_byte_len);
220 if (!ret) {
221 DEBUG(0, ("push_ucs2_talloc() for domain failed\n"));
222 talloc_free(mem_ctx);
223 return false;
226 SMB_ASSERT(user_byte_len >= 2);
227 SMB_ASSERT(domain_byte_len >= 2);
229 /* We don't want null termination */
230 user_byte_len = user_byte_len - 2;
231 domain_byte_len = domain_byte_len - 2;
233 hmac_md5_init_limK_to_64(owf, 16, &ctx);
234 hmac_md5_update((const void *)user, user_byte_len, &ctx);
235 hmac_md5_update((const void *)domain, domain_byte_len, &ctx);
236 hmac_md5_final(kr_buf, &ctx);
238 #ifdef DEBUG_PASSWORD
239 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
240 dump_data(100, (const void *)user, user_byte_len);
241 dump_data(100, (const void *)domain, domain_byte_len);
242 dump_data(100, owf, 16);
243 dump_data(100, kr_buf, 16);
244 #endif
246 talloc_free(mem_ctx);
247 return true;
250 /* Does the des encryption from the NT or LM MD4 hash. */
251 void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24])
253 uint8_t p21[21];
255 ZERO_STRUCT(p21);
257 memcpy(p21, passwd, 16);
258 E_P24(p21, c8, p24);
261 /* Does the des encryption. */
263 void SMBNTencrypt_hash(const uint8_t nt_hash[16], uint8_t *c8, uint8_t *p24)
265 uint8_t p21[21];
267 memset(p21,'\0',21);
268 memcpy(p21, nt_hash, 16);
269 SMBOWFencrypt(p21, c8, p24);
271 #ifdef DEBUG_PASSWORD
272 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
273 dump_data(100, p21, 16);
274 dump_data(100, c8, 8);
275 dump_data(100, p24, 24);
276 #endif
279 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
281 void SMBNTencrypt(const char *passwd, uint8_t *c8, uint8_t *p24)
283 uint8_t nt_hash[16];
284 E_md4hash(passwd, nt_hash);
285 SMBNTencrypt_hash(nt_hash, c8, p24);
289 /* Does the md5 encryption from the Key Response for NTLMv2. */
290 void SMBOWFencrypt_ntv2(const uint8_t kr[16],
291 const DATA_BLOB *srv_chal,
292 const DATA_BLOB *smbcli_chal,
293 uint8_t resp_buf[16])
295 HMACMD5Context ctx;
297 hmac_md5_init_limK_to_64(kr, 16, &ctx);
298 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
299 hmac_md5_update(smbcli_chal->data, smbcli_chal->length, &ctx);
300 hmac_md5_final(resp_buf, &ctx);
302 #ifdef DEBUG_PASSWORD
303 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));
304 dump_data(100, srv_chal->data, srv_chal->length);
305 dump_data(100, smbcli_chal->data, smbcli_chal->length);
306 dump_data(100, resp_buf, 16);
307 #endif
310 void SMBsesskeygen_ntv2(const uint8_t kr[16],
311 const uint8_t * nt_resp, uint8_t sess_key[16])
313 /* a very nice, 128 bit, variable session key */
315 HMACMD5Context ctx;
317 hmac_md5_init_limK_to_64(kr, 16, &ctx);
318 hmac_md5_update(nt_resp, 16, &ctx);
319 hmac_md5_final((uint8_t *)sess_key, &ctx);
321 #ifdef DEBUG_PASSWORD
322 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
323 dump_data(100, sess_key, 16);
324 #endif
327 void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16])
329 /* yes, this session key does not change - yes, this
330 is a problem - but it is 128 bits */
332 mdfour((uint8_t *)sess_key, kr, 16);
334 #ifdef DEBUG_PASSWORD
335 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
336 dump_data(100, sess_key, 16);
337 #endif
340 void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
341 const uint8_t lm_resp[24], /* only uses 8 */
342 uint8_t sess_key[16])
344 /* Calculate the LM session key (effective length 40 bits,
345 but changes with each session) */
346 uint8_t p24[24];
347 uint8_t partial_lm_hash[14];
349 memcpy(partial_lm_hash, lm_hash, 8);
350 memset(partial_lm_hash + 8, 0xbd, 6);
352 des_crypt56(p24, lm_resp, partial_lm_hash, 1);
353 des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
355 memcpy(sess_key, p24, 16);
357 #ifdef DEBUG_PASSWORD
358 DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
359 dump_data(100, sess_key, 16);
360 #endif
363 DATA_BLOB NTLMv2_generate_names_blob(TALLOC_CTX *mem_ctx,
364 const char *hostname,
365 const char *domain)
367 DATA_BLOB names_blob = data_blob_talloc(mem_ctx, NULL, 0);
369 msrpc_gen(mem_ctx, &names_blob,
370 "aaa",
371 NTLMSSP_NAME_TYPE_DOMAIN, domain,
372 NTLMSSP_NAME_TYPE_SERVER, hostname,
373 0, "");
374 return names_blob;
377 static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx, const DATA_BLOB *names_blob)
379 uint8_t client_chal[8];
380 DATA_BLOB response = data_blob(NULL, 0);
381 uint8_t long_date[8];
382 NTTIME nttime;
384 unix_to_nt_time(&nttime, time(NULL));
386 generate_random_buffer(client_chal, sizeof(client_chal));
388 push_nttime(long_date, 0, nttime);
390 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
392 msrpc_gen(mem_ctx, &response, "ddbbdb",
393 0x00000101, /* Header */
394 0, /* 'Reserved' */
395 long_date, 8, /* Timestamp */
396 client_chal, 8, /* client challenge */
397 0, /* Unknown */
398 names_blob->data, names_blob->length); /* End of name list */
400 return response;
403 static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx,
404 const uint8_t ntlm_v2_hash[16],
405 const DATA_BLOB *server_chal,
406 const DATA_BLOB *names_blob)
408 uint8_t ntlmv2_response[16];
409 DATA_BLOB ntlmv2_client_data;
410 DATA_BLOB final_response;
412 TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0,
413 "NTLMv2_generate_response internal context");
415 if (!mem_ctx) {
416 return data_blob(NULL, 0);
419 /* NTLMv2 */
420 /* generate some data to pass into the response function - including
421 the hostname and domain name of the server */
422 ntlmv2_client_data = NTLMv2_generate_client_data(mem_ctx, names_blob);
424 /* Given that data, and the challenge from the server, generate a response */
425 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
427 final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
429 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
431 memcpy(final_response.data+sizeof(ntlmv2_response),
432 ntlmv2_client_data.data, ntlmv2_client_data.length);
434 talloc_free(mem_ctx);
436 return final_response;
439 static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx,
440 const uint8_t ntlm_v2_hash[16],
441 const DATA_BLOB *server_chal)
443 uint8_t lmv2_response[16];
444 DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
445 DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
447 /* LMv2 */
448 /* client-supplied random data */
449 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length);
451 /* Given that data, and the challenge from the server, generate a response */
452 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
453 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
455 /* after the first 16 bytes is the random data we generated above,
456 so the server can verify us with it */
457 memcpy(final_response.data+sizeof(lmv2_response),
458 lmv2_client_data.data, lmv2_client_data.length);
460 data_blob_free(&lmv2_client_data);
462 return final_response;
465 bool SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx,
466 const char *user, const char *domain, const uint8_t nt_hash[16],
467 const DATA_BLOB *server_chal,
468 const DATA_BLOB *names_blob,
469 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
470 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
472 uint8_t ntlm_v2_hash[16];
474 /* We don't use the NT# directly. Instead we use it mashed up with
475 the username and domain.
476 This prevents username swapping during the auth exchange
478 if (!ntv2_owf_gen(nt_hash, user, domain, true, ntlm_v2_hash)) {
479 return false;
482 if (nt_response) {
483 *nt_response = NTLMv2_generate_response(mem_ctx,
484 ntlm_v2_hash, server_chal,
485 names_blob);
486 if (user_session_key) {
487 *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
489 /* The NTLMv2 calculations also provide a session key, for signing etc later */
490 /* use only the first 16 bytes of nt_response for session key */
491 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
495 /* LMv2 */
497 if (lm_response) {
498 *lm_response = LMv2_generate_response(mem_ctx,
499 ntlm_v2_hash, server_chal);
500 if (lm_session_key) {
501 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
503 /* The NTLMv2 calculations also provide a session key, for signing etc later */
504 /* use only the first 16 bytes of lm_response for session key */
505 SMBsesskeygen_ntv2(ntlm_v2_hash, lm_response->data, lm_session_key->data);
509 return true;
512 bool SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx,
513 const char *user, const char *domain,
514 const char *password,
515 const DATA_BLOB *server_chal,
516 const DATA_BLOB *names_blob,
517 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
518 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
520 uint8_t nt_hash[16];
521 E_md4hash(password, nt_hash);
523 return SMBNTLMv2encrypt_hash(mem_ctx,
524 user, domain, nt_hash, server_chal, names_blob,
525 lm_response, nt_response, lm_session_key, user_session_key);
528 /***********************************************************
529 encode a password buffer with a unicode password. The buffer
530 is filled with random data to make it harder to attack.
531 ************************************************************/
532 bool encode_pw_buffer(uint8_t buffer[516], const char *password, int string_flags)
534 uint8_t new_pw[512];
535 size_t new_pw_len;
537 /* the incoming buffer can be any alignment. */
538 string_flags |= STR_NOALIGN;
540 new_pw_len = push_string(new_pw,
541 password,
542 sizeof(new_pw), string_flags);
544 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
546 generate_random_buffer(buffer, 512 - new_pw_len);
549 * The length of the new password is in the last 4 bytes of
550 * the data buffer.
552 SIVAL(buffer, 512, new_pw_len);
553 ZERO_STRUCT(new_pw);
554 return true;
558 /***********************************************************
559 decode a password buffer
560 *new_pw_len is the length in bytes of the possibly mulitbyte
561 returned password including termination.
562 ************************************************************/
564 bool decode_pw_buffer(TALLOC_CTX *ctx,
565 uint8_t in_buffer[516],
566 char **pp_new_pwrd,
567 size_t *new_pw_len,
568 charset_t string_charset)
570 int byte_len=0;
572 *pp_new_pwrd = NULL;
573 *new_pw_len = 0;
576 Warning !!! : This function is called from some rpc call.
577 The password IN the buffer may be a UNICODE string.
578 The password IN new_pwrd is an ASCII string
579 If you reuse that code somewhere else check first.
582 /* The length of the new password is in the last 4 bytes of the data buffer. */
584 byte_len = IVAL(in_buffer, 512);
586 #ifdef DEBUG_PASSWORD
587 dump_data(100, in_buffer, 516);
588 #endif
590 /* Password cannot be longer than the size of the password buffer */
591 if ( (byte_len < 0) || (byte_len > 512)) {
592 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
593 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
594 return false;
597 /* decode into the return buffer. */
598 if (!convert_string_talloc(ctx, string_charset, CH_UNIX,
599 &in_buffer[512 - byte_len],
600 byte_len,
601 (void *)pp_new_pwrd,
602 new_pw_len,
603 false)) {
604 DEBUG(0, ("decode_pw_buffer: failed to convert incoming password\n"));
605 return false;
608 #ifdef DEBUG_PASSWORD
609 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
610 dump_data(100, (uint8_t *)*pp_new_pwrd, *new_pw_len);
611 DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len));
612 DEBUG(100,("original char len:%d\n", byte_len/2));
613 #endif
615 return true;
618 /***********************************************************
619 Decode an arc4 encrypted password change buffer.
620 ************************************************************/
622 void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
624 struct MD5Context tctx;
625 unsigned char key_out[16];
627 /* Confounder is last 16 bytes. */
629 MD5Init(&tctx);
630 MD5Update(&tctx, &pw_buf[516], 16);
631 MD5Update(&tctx, psession_key->data, psession_key->length);
632 MD5Final(key_out, &tctx);
633 /* arc4 with key_out. */
634 arcfour_crypt(pw_buf, key_out, 516);
637 /***********************************************************
638 encode a password buffer with an already unicode password. The
639 rest of the buffer is filled with random data to make it harder to attack.
640 ************************************************************/
641 bool set_pw_in_buffer(uint8_t buffer[516], DATA_BLOB *password)
643 if (password->length > 512) {
644 return false;
647 memcpy(&buffer[512 - password->length], password->data, password->length);
649 generate_random_buffer(buffer, 512 - password->length);
652 * The length of the new password is in the last 4 bytes of
653 * the data buffer.
655 SIVAL(buffer, 512, password->length);
656 return true;
659 /***********************************************************
660 decode a password buffer
661 *new_pw_size is the length in bytes of the extracted unicode password
662 ************************************************************/
663 bool extract_pw_from_buffer(TALLOC_CTX *mem_ctx,
664 uint8_t in_buffer[516], DATA_BLOB *new_pass)
666 int byte_len=0;
668 /* The length of the new password is in the last 4 bytes of the data buffer. */
670 byte_len = IVAL(in_buffer, 512);
672 #ifdef DEBUG_PASSWORD
673 dump_data(100, in_buffer, 516);
674 #endif
676 /* Password cannot be longer than the size of the password buffer */
677 if ( (byte_len < 0) || (byte_len > 512)) {
678 return false;
681 *new_pass = data_blob_talloc(mem_ctx, &in_buffer[512 - byte_len], byte_len);
683 if (!new_pass->data) {
684 return false;
687 return true;
691 /* encode a wkssvc_PasswordBuffer:
693 * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
694 * 16byte), confounder in front of the 516 byte buffer (instead of after that
695 * buffer), calling MD5Update() first with session_key and then with confounder
696 * (vice versa in samr) - Guenther */
698 void encode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
699 const char *pwd,
700 DATA_BLOB *session_key,
701 struct wkssvc_PasswordBuffer **pwd_buf)
703 uint8_t buffer[516];
704 struct MD5Context ctx;
705 struct wkssvc_PasswordBuffer *my_pwd_buf = NULL;
706 DATA_BLOB confounded_session_key;
707 int confounder_len = 8;
708 uint8_t confounder[8];
710 my_pwd_buf = talloc_zero(mem_ctx, struct wkssvc_PasswordBuffer);
711 if (!my_pwd_buf) {
712 return;
715 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
717 encode_pw_buffer(buffer, pwd, STR_UNICODE);
719 generate_random_buffer((uint8_t *)confounder, confounder_len);
721 MD5Init(&ctx);
722 MD5Update(&ctx, session_key->data, session_key->length);
723 MD5Update(&ctx, confounder, confounder_len);
724 MD5Final(confounded_session_key.data, &ctx);
726 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
728 memcpy(&my_pwd_buf->data[0], confounder, confounder_len);
729 memcpy(&my_pwd_buf->data[8], buffer, 516);
731 data_blob_free(&confounded_session_key);
733 *pwd_buf = my_pwd_buf;
736 WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
737 struct wkssvc_PasswordBuffer *pwd_buf,
738 DATA_BLOB *session_key,
739 char **pwd)
741 uint8_t buffer[516];
742 struct MD5Context ctx;
743 size_t pwd_len;
745 DATA_BLOB confounded_session_key;
747 int confounder_len = 8;
748 uint8_t confounder[8];
750 *pwd = NULL;
752 if (!pwd_buf) {
753 return WERR_BAD_PASSWORD;
756 if (session_key->length != 16) {
757 DEBUG(10,("invalid session key\n"));
758 return WERR_BAD_PASSWORD;
761 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
763 memcpy(&confounder, &pwd_buf->data[0], confounder_len);
764 memcpy(&buffer, &pwd_buf->data[8], 516);
766 MD5Init(&ctx);
767 MD5Update(&ctx, session_key->data, session_key->length);
768 MD5Update(&ctx, confounder, confounder_len);
769 MD5Final(confounded_session_key.data, &ctx);
771 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
773 if (!decode_pw_buffer(mem_ctx, buffer, pwd, &pwd_len, CH_UTF16)) {
774 data_blob_free(&confounded_session_key);
775 return WERR_BAD_PASSWORD;
778 data_blob_free(&confounded_session_key);
780 return WERR_OK;