s3-torture: run_fdpasstest(): replace cli_read_old() with cli_read()
[Samba/vl.git] / libcli / auth / smbencrypt.c
blob66fdbd25a80ffdaa2e546c775217d6d4b1ce56d1
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"
29 #include "../librpc/gen_ndr/ntlmssp.h"
31 void SMBencrypt_hash(const uint8_t lm_hash[16], const uint8_t *c8, uint8_t p24[24])
33 uint8_t p21[21];
35 memset(p21,'\0',21);
36 memcpy(p21, lm_hash, 16);
38 SMBOWFencrypt(p21, c8, p24);
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
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
53 Returns False if password must have been truncated to create LM hash
56 bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24])
58 bool ret;
59 uint8_t lm_hash[16];
61 ret = E_deshash(passwd, lm_hash);
62 SMBencrypt_hash(lm_hash, c8, p24);
63 return ret;
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
72 bool E_md4hash(const char *passwd, uint8_t p16[16])
74 size_t len;
75 smb_ucs2_t *wpwd;
76 bool ret;
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;
86 len -= 2;
87 mdfour(p16, (const uint8_t *)wpwd, len);
89 talloc_free(wpwd);
90 return true;
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
100 void E_md5hash(const uint8_t salt[16], const uint8_t nthash[16], uint8_t hash_out[16])
102 struct MD5Context tctx;
103 MD5Init(&tctx);
104 MD5Update(&tctx, salt, 16);
105 MD5Update(&tctx, nthash, 16);
106 MD5Final(hash_out, &tctx);
110 * Creates the DES forward-only Hash of the users password in DOS ASCII charset
111 * @param passwd password in 'unix' charset.
112 * @param p16 return password hashed with DES, caller allocated 16 byte buffer
113 * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true
114 * @note p16 is filled in regardless
117 bool E_deshash(const char *passwd, uint8_t p16[16])
119 bool ret;
120 uint8_t dospwd[14];
121 TALLOC_CTX *frame = talloc_stackframe();
123 size_t converted_size;
125 char *tmpbuf;
127 ZERO_STRUCT(dospwd);
129 tmpbuf = strupper_talloc(frame, passwd);
130 if (tmpbuf == NULL) {
131 /* Too many callers don't check this result, we need to fill in the buffer with something */
132 strlcpy((char *)dospwd, passwd ? passwd : "", sizeof(dospwd));
133 E_P16(dospwd, p16);
134 talloc_free(frame);
135 return false;
138 ZERO_STRUCT(dospwd);
140 ret = convert_string_error(CH_UNIX, CH_DOS, tmpbuf, strlen(tmpbuf), dospwd, sizeof(dospwd), &converted_size);
141 talloc_free(frame);
143 /* Only the first 14 chars are considered, password need not
144 * be null terminated. We do this in the error and success
145 * case to avoid returning a fixed 'password' buffer, but
146 * callers should not use it when E_deshash returns false */
148 E_P16((const uint8_t *)dospwd, p16);
150 ZERO_STRUCT(dospwd);
152 return ret;
156 * Creates the MD4 and DES (LM) Hash of the users password.
157 * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
158 * @param passwd password in 'unix' charset.
159 * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
160 * @param p16 return password hashed with des, caller allocated 16 byte buffer
163 /* Does both the NT and LM owfs of a user's password */
164 void nt_lm_owf_gen(const char *pwd, uint8_t nt_p16[16], uint8_t p16[16])
166 /* Calculate the MD4 hash (NT compatible) of the password */
167 memset(nt_p16, '\0', 16);
168 E_md4hash(pwd, nt_p16);
170 #ifdef DEBUG_PASSWORD
171 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
172 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
173 dump_data(100, nt_p16, 16);
174 #endif
176 E_deshash(pwd, (uint8_t *)p16);
178 #ifdef DEBUG_PASSWORD
179 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
180 dump_data(120, (const uint8_t *)pwd, strlen(pwd));
181 dump_data(100, p16, 16);
182 #endif
185 /* Does both the NTLMv2 owfs of a user's password */
186 bool ntv2_owf_gen(const uint8_t owf[16],
187 const char *user_in, const char *domain_in,
188 bool upper_case_domain, /* Transform the domain into UPPER case */
189 uint8_t kr_buf[16])
191 smb_ucs2_t *user;
192 smb_ucs2_t *domain;
193 size_t user_byte_len;
194 size_t domain_byte_len;
195 bool ret;
197 HMACMD5Context ctx;
198 TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in);
200 if (!mem_ctx) {
201 return false;
204 if (!user_in) {
205 user_in = "";
208 if (!domain_in) {
209 domain_in = "";
212 user_in = strupper_talloc(mem_ctx, user_in);
213 if (user_in == NULL) {
214 talloc_free(mem_ctx);
215 return false;
218 if (upper_case_domain) {
219 domain_in = strupper_talloc(mem_ctx, domain_in);
220 if (domain_in == NULL) {
221 talloc_free(mem_ctx);
222 return false;
226 ret = push_ucs2_talloc(mem_ctx, &user, user_in, &user_byte_len );
227 if (!ret) {
228 DEBUG(0, ("push_uss2_talloc() for user failed)\n"));
229 talloc_free(mem_ctx);
230 return false;
233 ret = push_ucs2_talloc(mem_ctx, &domain, domain_in, &domain_byte_len);
234 if (!ret) {
235 DEBUG(0, ("push_ucs2_talloc() for domain failed\n"));
236 talloc_free(mem_ctx);
237 return false;
240 SMB_ASSERT(user_byte_len >= 2);
241 SMB_ASSERT(domain_byte_len >= 2);
243 /* We don't want null termination */
244 user_byte_len = user_byte_len - 2;
245 domain_byte_len = domain_byte_len - 2;
247 hmac_md5_init_limK_to_64(owf, 16, &ctx);
248 hmac_md5_update((uint8_t *)user, user_byte_len, &ctx);
249 hmac_md5_update((uint8_t *)domain, domain_byte_len, &ctx);
250 hmac_md5_final(kr_buf, &ctx);
252 #ifdef DEBUG_PASSWORD
253 DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
254 dump_data(100, (uint8_t *)user, user_byte_len);
255 dump_data(100, (uint8_t *)domain, domain_byte_len);
256 dump_data(100, owf, 16);
257 dump_data(100, kr_buf, 16);
258 #endif
260 talloc_free(mem_ctx);
261 return true;
264 /* Does the des encryption from the NT or LM MD4 hash. */
265 void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24])
267 uint8_t p21[21];
269 ZERO_STRUCT(p21);
271 memcpy(p21, passwd, 16);
272 E_P24(p21, c8, p24);
275 /* Does the des encryption. */
277 void SMBNTencrypt_hash(const uint8_t nt_hash[16], uint8_t *c8, uint8_t *p24)
279 uint8_t p21[21];
281 memset(p21,'\0',21);
282 memcpy(p21, nt_hash, 16);
283 SMBOWFencrypt(p21, c8, p24);
285 #ifdef DEBUG_PASSWORD
286 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
287 dump_data(100, p21, 16);
288 dump_data(100, c8, 8);
289 dump_data(100, p24, 24);
290 #endif
293 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
295 void SMBNTencrypt(const char *passwd, uint8_t *c8, uint8_t *p24)
297 uint8_t nt_hash[16];
298 E_md4hash(passwd, nt_hash);
299 SMBNTencrypt_hash(nt_hash, c8, p24);
303 /* Does the md5 encryption from the Key Response for NTLMv2. */
304 void SMBOWFencrypt_ntv2(const uint8_t kr[16],
305 const DATA_BLOB *srv_chal,
306 const DATA_BLOB *smbcli_chal,
307 uint8_t resp_buf[16])
309 HMACMD5Context ctx;
311 hmac_md5_init_limK_to_64(kr, 16, &ctx);
312 hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
313 hmac_md5_update(smbcli_chal->data, smbcli_chal->length, &ctx);
314 hmac_md5_final(resp_buf, &ctx);
316 #ifdef DEBUG_PASSWORD
317 DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));
318 dump_data(100, srv_chal->data, srv_chal->length);
319 dump_data(100, smbcli_chal->data, smbcli_chal->length);
320 dump_data(100, resp_buf, 16);
321 #endif
324 void SMBsesskeygen_ntv2(const uint8_t kr[16],
325 const uint8_t * nt_resp, uint8_t sess_key[16])
327 /* a very nice, 128 bit, variable session key */
329 HMACMD5Context ctx;
331 hmac_md5_init_limK_to_64(kr, 16, &ctx);
332 hmac_md5_update(nt_resp, 16, &ctx);
333 hmac_md5_final((uint8_t *)sess_key, &ctx);
335 #ifdef DEBUG_PASSWORD
336 DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
337 dump_data(100, sess_key, 16);
338 #endif
341 void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16])
343 /* yes, this session key does not change - yes, this
344 is a problem - but it is 128 bits */
346 mdfour((uint8_t *)sess_key, kr, 16);
348 #ifdef DEBUG_PASSWORD
349 DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
350 dump_data(100, sess_key, 16);
351 #endif
354 void SMBsesskeygen_lm_sess_key(const uint8_t lm_hash[16],
355 const uint8_t lm_resp[24], /* only uses 8 */
356 uint8_t sess_key[16])
358 /* Calculate the LM session key (effective length 40 bits,
359 but changes with each session) */
360 uint8_t p24[24];
361 uint8_t partial_lm_hash[14];
363 memcpy(partial_lm_hash, lm_hash, 8);
364 memset(partial_lm_hash + 8, 0xbd, 6);
366 des_crypt56(p24, lm_resp, partial_lm_hash, 1);
367 des_crypt56(p24+8, lm_resp, partial_lm_hash + 7, 1);
369 memcpy(sess_key, p24, 16);
371 #ifdef DEBUG_PASSWORD
372 DEBUG(100, ("SMBsesskeygen_lm_sess_key: \n"));
373 dump_data(100, sess_key, 16);
374 #endif
377 DATA_BLOB NTLMv2_generate_names_blob(TALLOC_CTX *mem_ctx,
378 const char *hostname,
379 const char *domain)
381 DATA_BLOB names_blob = data_blob_talloc(mem_ctx, NULL, 0);
383 /* Deliberately ignore return here.. */
384 if (hostname != NULL) {
385 (void)msrpc_gen(mem_ctx, &names_blob,
386 "aaa",
387 MsvAvNbDomainName, domain,
388 MsvAvNbComputerName, hostname,
389 MsvAvEOL, "");
390 } else {
391 (void)msrpc_gen(mem_ctx, &names_blob,
392 "aa",
393 MsvAvNbDomainName, domain,
394 MsvAvEOL, "");
396 return names_blob;
399 static DATA_BLOB NTLMv2_generate_client_data(TALLOC_CTX *mem_ctx, const DATA_BLOB *names_blob)
401 uint8_t client_chal[8];
402 DATA_BLOB response = data_blob(NULL, 0);
403 uint8_t long_date[8];
404 NTTIME nttime;
406 unix_to_nt_time(&nttime, time(NULL));
408 generate_random_buffer(client_chal, sizeof(client_chal));
410 push_nttime(long_date, 0, nttime);
412 /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
414 /* Deliberately ignore return here.. */
415 (void)msrpc_gen(mem_ctx, &response, "ddbbdb",
416 0x00000101, /* Header */
417 0, /* 'Reserved' */
418 long_date, 8, /* Timestamp */
419 client_chal, 8, /* client challenge */
420 0, /* Unknown */
421 names_blob->data, names_blob->length); /* End of name list */
423 return response;
426 static DATA_BLOB NTLMv2_generate_response(TALLOC_CTX *out_mem_ctx,
427 const uint8_t ntlm_v2_hash[16],
428 const DATA_BLOB *server_chal,
429 const DATA_BLOB *names_blob)
431 uint8_t ntlmv2_response[16];
432 DATA_BLOB ntlmv2_client_data;
433 DATA_BLOB final_response;
435 TALLOC_CTX *mem_ctx = talloc_named(out_mem_ctx, 0,
436 "NTLMv2_generate_response internal context");
438 if (!mem_ctx) {
439 return data_blob(NULL, 0);
442 /* NTLMv2 */
443 /* generate some data to pass into the response function - including
444 the hostname and domain name of the server */
445 ntlmv2_client_data = NTLMv2_generate_client_data(mem_ctx, names_blob);
447 /* Given that data, and the challenge from the server, generate a response */
448 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
450 final_response = data_blob_talloc(out_mem_ctx, NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
452 memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
454 memcpy(final_response.data+sizeof(ntlmv2_response),
455 ntlmv2_client_data.data, ntlmv2_client_data.length);
457 talloc_free(mem_ctx);
459 return final_response;
462 static DATA_BLOB LMv2_generate_response(TALLOC_CTX *mem_ctx,
463 const uint8_t ntlm_v2_hash[16],
464 const DATA_BLOB *server_chal)
466 uint8_t lmv2_response[16];
467 DATA_BLOB lmv2_client_data = data_blob_talloc(mem_ctx, NULL, 8);
468 DATA_BLOB final_response = data_blob_talloc(mem_ctx, NULL,24);
470 /* LMv2 */
471 /* client-supplied random data */
472 generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length);
474 /* Given that data, and the challenge from the server, generate a response */
475 SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
476 memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
478 /* after the first 16 bytes is the random data we generated above,
479 so the server can verify us with it */
480 memcpy(final_response.data+sizeof(lmv2_response),
481 lmv2_client_data.data, lmv2_client_data.length);
483 data_blob_free(&lmv2_client_data);
485 return final_response;
488 bool SMBNTLMv2encrypt_hash(TALLOC_CTX *mem_ctx,
489 const char *user, const char *domain, const uint8_t nt_hash[16],
490 const DATA_BLOB *server_chal,
491 const DATA_BLOB *names_blob,
492 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
493 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
495 uint8_t ntlm_v2_hash[16];
497 /* We don't use the NT# directly. Instead we use it mashed up with
498 the username and domain.
499 This prevents username swapping during the auth exchange
501 if (!ntv2_owf_gen(nt_hash, user, domain, true, ntlm_v2_hash)) {
502 return false;
505 if (nt_response) {
506 *nt_response = NTLMv2_generate_response(mem_ctx,
507 ntlm_v2_hash, server_chal,
508 names_blob);
509 if (user_session_key) {
510 *user_session_key = data_blob_talloc(mem_ctx, NULL, 16);
512 /* The NTLMv2 calculations also provide a session key, for signing etc later */
513 /* use only the first 16 bytes of nt_response for session key */
514 SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
518 /* LMv2 */
520 if (lm_response) {
521 *lm_response = LMv2_generate_response(mem_ctx,
522 ntlm_v2_hash, server_chal);
523 if (lm_session_key) {
524 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
526 /* The NTLMv2 calculations also provide a session key, for signing etc later */
527 /* use only the first 16 bytes of lm_response for session key */
528 SMBsesskeygen_ntv2(ntlm_v2_hash, lm_response->data, lm_session_key->data);
532 return true;
535 bool SMBNTLMv2encrypt(TALLOC_CTX *mem_ctx,
536 const char *user, const char *domain,
537 const char *password,
538 const DATA_BLOB *server_chal,
539 const DATA_BLOB *names_blob,
540 DATA_BLOB *lm_response, DATA_BLOB *nt_response,
541 DATA_BLOB *lm_session_key, DATA_BLOB *user_session_key)
543 uint8_t nt_hash[16];
544 E_md4hash(password, nt_hash);
546 return SMBNTLMv2encrypt_hash(mem_ctx,
547 user, domain, nt_hash, server_chal, names_blob,
548 lm_response, nt_response, lm_session_key, user_session_key);
551 /***********************************************************
552 encode a password buffer with a unicode password. The buffer
553 is filled with random data to make it harder to attack.
554 ************************************************************/
555 bool encode_pw_buffer(uint8_t buffer[516], const char *password, int string_flags)
557 uint8_t new_pw[512];
558 ssize_t new_pw_len;
560 /* the incoming buffer can be any alignment. */
561 string_flags |= STR_NOALIGN;
563 new_pw_len = push_string(new_pw,
564 password,
565 sizeof(new_pw), string_flags);
566 if (new_pw_len == -1) {
567 return false;
570 memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
572 generate_random_buffer(buffer, 512 - new_pw_len);
575 * The length of the new password is in the last 4 bytes of
576 * the data buffer.
578 SIVAL(buffer, 512, new_pw_len);
579 ZERO_STRUCT(new_pw);
580 return true;
584 /***********************************************************
585 decode a password buffer
586 *new_pw_len is the length in bytes of the possibly mulitbyte
587 returned password including termination.
588 ************************************************************/
590 bool decode_pw_buffer(TALLOC_CTX *ctx,
591 uint8_t in_buffer[516],
592 char **pp_new_pwrd,
593 size_t *new_pw_len,
594 charset_t string_charset)
596 int byte_len=0;
598 *pp_new_pwrd = NULL;
599 *new_pw_len = 0;
602 Warning !!! : This function is called from some rpc call.
603 The password IN the buffer may be a UNICODE string.
604 The password IN new_pwrd is an ASCII string
605 If you reuse that code somewhere else check first.
608 /* The length of the new password is in the last 4 bytes of the data buffer. */
610 byte_len = IVAL(in_buffer, 512);
612 #ifdef DEBUG_PASSWORD
613 dump_data(100, in_buffer, 516);
614 #endif
616 /* Password cannot be longer than the size of the password buffer */
617 if ( (byte_len < 0) || (byte_len > 512)) {
618 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
619 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
620 return false;
623 /* decode into the return buffer. */
624 if (!convert_string_talloc(ctx, string_charset, CH_UNIX,
625 &in_buffer[512 - byte_len],
626 byte_len,
627 (void *)pp_new_pwrd,
628 new_pw_len)) {
629 DEBUG(0, ("decode_pw_buffer: failed to convert incoming password\n"));
630 return false;
633 #ifdef DEBUG_PASSWORD
634 DEBUG(100,("decode_pw_buffer: new_pwrd: "));
635 dump_data(100, (uint8_t *)*pp_new_pwrd, *new_pw_len);
636 DEBUG(100,("multibyte len:%lu\n", (unsigned long int)*new_pw_len));
637 DEBUG(100,("original char len:%d\n", byte_len/2));
638 #endif
640 return true;
643 /***********************************************************
644 Decode an arc4 encrypted password change buffer.
645 ************************************************************/
647 void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
649 struct MD5Context tctx;
650 unsigned char key_out[16];
652 /* Confounder is last 16 bytes. */
654 MD5Init(&tctx);
655 MD5Update(&tctx, &pw_buf[516], 16);
656 MD5Update(&tctx, psession_key->data, psession_key->length);
657 MD5Final(key_out, &tctx);
658 /* arc4 with key_out. */
659 arcfour_crypt(pw_buf, key_out, 516);
662 /***********************************************************
663 encode a password buffer with an already unicode password. The
664 rest of the buffer is filled with random data to make it harder to attack.
665 ************************************************************/
666 bool set_pw_in_buffer(uint8_t buffer[516], DATA_BLOB *password)
668 if (password->length > 512) {
669 return false;
672 memcpy(&buffer[512 - password->length], password->data, password->length);
674 generate_random_buffer(buffer, 512 - password->length);
677 * The length of the new password is in the last 4 bytes of
678 * the data buffer.
680 SIVAL(buffer, 512, password->length);
681 return true;
684 /***********************************************************
685 decode a password buffer
686 *new_pw_size is the length in bytes of the extracted unicode password
687 ************************************************************/
688 bool extract_pw_from_buffer(TALLOC_CTX *mem_ctx,
689 uint8_t in_buffer[516], DATA_BLOB *new_pass)
691 int byte_len=0;
693 /* The length of the new password is in the last 4 bytes of the data buffer. */
695 byte_len = IVAL(in_buffer, 512);
697 #ifdef DEBUG_PASSWORD
698 dump_data(100, in_buffer, 516);
699 #endif
701 /* Password cannot be longer than the size of the password buffer */
702 if ( (byte_len < 0) || (byte_len > 512)) {
703 return false;
706 *new_pass = data_blob_talloc(mem_ctx, &in_buffer[512 - byte_len], byte_len);
708 if (!new_pass->data) {
709 return false;
712 return true;
716 /* encode a wkssvc_PasswordBuffer:
718 * similar to samr_CryptPasswordEx. Different: 8byte confounder (instead of
719 * 16byte), confounder in front of the 516 byte buffer (instead of after that
720 * buffer), calling MD5Update() first with session_key and then with confounder
721 * (vice versa in samr) - Guenther */
723 void encode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
724 const char *pwd,
725 DATA_BLOB *session_key,
726 struct wkssvc_PasswordBuffer **pwd_buf)
728 uint8_t buffer[516];
729 struct MD5Context ctx;
730 struct wkssvc_PasswordBuffer *my_pwd_buf = NULL;
731 DATA_BLOB confounded_session_key;
732 int confounder_len = 8;
733 uint8_t confounder[8];
735 my_pwd_buf = talloc_zero(mem_ctx, struct wkssvc_PasswordBuffer);
736 if (!my_pwd_buf) {
737 return;
740 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
742 encode_pw_buffer(buffer, pwd, STR_UNICODE);
744 generate_random_buffer((uint8_t *)confounder, confounder_len);
746 MD5Init(&ctx);
747 MD5Update(&ctx, session_key->data, session_key->length);
748 MD5Update(&ctx, confounder, confounder_len);
749 MD5Final(confounded_session_key.data, &ctx);
751 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
753 memcpy(&my_pwd_buf->data[0], confounder, confounder_len);
754 memcpy(&my_pwd_buf->data[8], buffer, 516);
756 data_blob_free(&confounded_session_key);
758 *pwd_buf = my_pwd_buf;
761 WERROR decode_wkssvc_join_password_buffer(TALLOC_CTX *mem_ctx,
762 struct wkssvc_PasswordBuffer *pwd_buf,
763 DATA_BLOB *session_key,
764 char **pwd)
766 uint8_t buffer[516];
767 struct MD5Context ctx;
768 size_t pwd_len;
770 DATA_BLOB confounded_session_key;
772 int confounder_len = 8;
773 uint8_t confounder[8];
775 *pwd = NULL;
777 if (!pwd_buf) {
778 return WERR_BAD_PASSWORD;
781 if (session_key->length != 16) {
782 DEBUG(10,("invalid session key\n"));
783 return WERR_BAD_PASSWORD;
786 confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
788 memcpy(&confounder, &pwd_buf->data[0], confounder_len);
789 memcpy(&buffer, &pwd_buf->data[8], 516);
791 MD5Init(&ctx);
792 MD5Update(&ctx, session_key->data, session_key->length);
793 MD5Update(&ctx, confounder, confounder_len);
794 MD5Final(confounded_session_key.data, &ctx);
796 arcfour_crypt_blob(buffer, 516, &confounded_session_key);
798 if (!decode_pw_buffer(mem_ctx, buffer, pwd, &pwd_len, CH_UTF16)) {
799 data_blob_free(&confounded_session_key);
800 return WERR_BAD_PASSWORD;
803 data_blob_free(&confounded_session_key);
805 return WERR_OK;